You are on page 1of 48

Basic Terminologies and Representations

Traversal algorithms
Define a graph G = (V, E) by defining a pair of ◦Denote vertices with labels
sets:
A B
◦V = a set of vertices
Representation:
◦E = a set of edges
◦Represent vertices with circles, perhaps
containing a label
Edges: ◦Represent edges with lines between circles
◦ Each edge is defined by a pair of vertices
◦ An edge connects the vertices that define it Example:
◦ In some cases, the vertices can be the same ◦ V = {A,B,C,D}
◦ E = {(A,B),(A,C),(A,D),(B,D),(C,D)}
Vertices: A
◦Vertices also called nodes
Contd.
.
Kinds of Choose the kind
graphs
1. Weighted(edges have a weight) or unweighted (edges have no weight) required for problem
◦ Weighted : typically shows cost of traversing and determined by data
5
◦ Example: weights are distances between cities A B

◦ Unweighted: Edges simply show connections


◦ Example: course prereqs A B

2. Directed or undirected
◦ Undirected Graphs:
◦ each edge can be traversed in either direction
◦no implied direction on edge between nodes
◦ Directed Graphs:
◦ each edge can be traversed only in a specified direction
◦ an edge is an unordered pair
Contd.
.
3. Cyclic or acyclic
A Cyclic graph contains cycles
◦Example: roads (normally)

An acyclic graph contains no cycles


◦Example: Course prereqs

Connected and Unconnected Graphs and Connected Components


 An undirected graph is connected if every pair of vertices has a path between it
 Otherwise it is unconnected

 An unconnected graph can be broken in to connected components

 A directed graph is strongly connected if every pair of vertices has a path between them, in both directions
Contd.
.
The degree of a node is the number of edges the node is used to define
Example
◦Degree 2: B and C
◦Degree 3: A and D
A and D have odd degree, and B and C have even degree
Can also define in-degree and out-degree
◦In-degree: Number of edges pointing to a node
◦Out-degree: Number of edges pointing from a node
Graphs: Terminology Involving
Paths
Path: sequence of vertices in which each pair of successive vertices is connected by an edge
Cycle: a path that starts and ends on the same vertex
Simple path: a path that does not cross itself
◦That is, no vertex is repeated (except first and last)
◦Simple paths cannot contain cycles

Length of a path: Number of edges in the path


◦Sometimes the sum of the weights of the edges
Data structures for representing
graphs
Adjacency lists
◦ Each node has a list of adjacent nodes
◦ Example (undirected graph):
◦ A: B, C, D
◦ B: A, D
◦ C: A, D
◦ D: A, B, C

Adjacency Matrix: 2D array containing weights on edges


◦ Row for each vertex
◦ Column for each vertex
◦ Entries contain weight of edge from row vertex to column
vertex
◦ Entries contain ∞ if no edge from row vertex to column vertex
◦ Entries contain 0 on diagonal (if self edges not allowed)
Adjacency matrix
Example (directed graph):assuming self edge is allowed
◦A: B, C, D
◦B: D
◦C: Nil
◦D: C

Can store weights in cells


Space: Θ(V2)
Time:
◦To visit each node
that is adjacent to
node u: Θ(V)
◦To determine if node
u is adjacent to node
Depth First
Search
An algorithm for traversing or searching tree or graph data structures

It explores all the nodes by going forward or It uses the idea of backtracking
It can be implemented using stack

The time complexity :O(V) where V is the number of nodes.

Steps:
1. Pick a node and push all its adjacent nodes into a stack.
2. Pop a node from the stack and push all its adjacent nodes into a stack.
3. Repeat this process until the stack is empty or you meet a goal.
Example DFS implementation using stack.
G
Breadth First
Search
An algorithm for traversing or searching tree or graph data structures.

It explores all the nodes at the present depth before moving on to the nodes at the next depth level.
It can be implemented using a queue.

The time complexity : O(V) where V is the number of nodes.

Steps:

1. Pick a node and enqueue all its adjacent nodes into a queue.
2. Dequeue a node from the queue, mark it as visited and enqueue all its adjacent nodes into a queue.
3. Repeat this process until the queue is empty or you meet a goal.
Shortest path problem

The problem of finding a path between two vertices (or nodes) in a graph
The sum of the weights of its constituent edges is minimized.
All pair shortest problem
◦ Single execution of the algorithm will find the lengths (summed
weights) of shortest paths between all pairs of vertices.
◦ Warshall’s Algorithm and Floyd’s Algorithm
◦The algorithm can also be used for finding the transitive closure of a relation (R)
of directed graph.
• given : directed graph G = ( V, E ),
construct : create an n × n matrix D = ( dij ) of shortest
path distances i.e., dij = ( vi , vj )
solution : Execute the algorithm n times, one for every vertex as the
source.
example

Given digraph. Find the transitive closure.

Adjacency Matrix
Digraph
Exampl
e
Given a digraph. Find the transitive closure using floyd’ss algorithm.

Distance Matrix
Digraph
Trees and Minimum Spanning
Trees
Tree: undirected, connected graph with no cycles
◦Example : If G=(V, E) is a tree, how many edges in
G?

Spanning tree: a spanning tree of G is a connected


subgraph of G that is a tree
Minimum spanning tree (MST): a spanning tree with
minimum weight
Spanning trees and minimum spanning tree are not
necessarily unique
In a weighted graph, a minimum spanning tree is a
spanning tree that has minimum weight than all
other spanning trees of the same graph.
Contd.
.
Properties of Spanning Tree
Spanning tree has n-1 edges, where n is the number of nodes (vertices).
From a complete graph, by removing maximum e - n + 1 edges, we can construct a spanning tree.
A complete graph can have maximum nn-2 number of spanning trees.

Application of Spanning Tree


Civil Network Planning
Computer Network Routing Protocol
Cluster Analysis
To
Possible MST of the given graph are ?
remember

Types of graph
Kruskal’s
algorithm
It forms a tree with every vertex in it.

The sum of the weights is the minimum among all the spanning trees that can
be formed from this graph.
Steps for Kruskal’s algorithm :
1. First sort all the edges from the lowest weight to highest.
2. Take edge with the lowest weight and add it to the spanning tree. If the
cycle is created, discard the edge.
3.Keep adding edges like in step 1 until all the vertices are considered.
Example : Find MST for the given graph:
Contd.
1. .choose the edge with the least weight which is
2-4.
2. choose the next shortest edge 2-3.
3. choose next edge with the shortest edge and
that does not create a cycle i.e. 0-3
4. choose the shortest edge so that it doesn’t
form a cycle. This is 0-1.

MST that include all the vertices


and with minimum cost

© Kalasalingam academy of research and education DATA STRUCTURES AND ALGORITHM


Prim’s
Algorithm
Steps to perform Prim’s Algorithm:
1. Choose a random node and highlight it
2. Find all of the edges that go to un-highlighted nodes. Highlight the edge with the lowest weight.
3. Highlight the node you just reached
4. Look at all of the nodes highlighted so far. Highlight the edge with lowest weight
5. Highlight the node you just reached
6. Highlight the edge with the lowest weight. Choose from all of the edges that:
1. Come from all of the highlighted nodes
2. Reach a node that you haven’t highlighted yet

7. Repeat steps 5 and 6 until you have no more un-highlighted nodes.

Note: if more than one edge with the same weight, pick a random one.
Contd.
.
Topological
Sort
A directed graph and returns an array of the nodes where each node appears before all the nodes it
points to.
 sorting is possible if the graph is directed acyclic graph.
The ordering of the nodes in the array is called a topological ordering.

 May exist multiple different topological orderings.

Applications:
 Scheduling jobs from the given dependencies among jobs
 Instruction Scheduling
 Determining the order of compilation tasks to perform in make files
 Data Serialization
Contd.
.
Example:
Find the number of different topological orderings possible for the given graph
1. specify in-degree of all vertex
Contd.
.
2. least in-degree vertex and its associated edges are removed. Update in-degree of all remaining
vertices.
3. Vertices with the same least in-degree. Continue with all cases separately by applying step 2.
4. repeat step 2 for all cases
5. repeat step 2 for all cases
Since in previous step, 2 vertices with the least in-degree. So, 2 options are possible. Any of the two
vertices may be taken first.
This is applicable for both cases.
Contd.
.
Solution :
The given graph can have 4 different topological ordering:

123456
123465
132456
132465

You might also like