You are on page 1of 16

CS6301-Prog.

& Data Structures II

5.1
UNIT V

GRAPHS

Representation of Graphs Breadth-first search Depth-first search Topological sort


Minimum Spanning Trees Kruskal and Prim algorithm Shortest path algorithm
Dijkstras algorithm Bellman-Ford algorithm Floyd - Warshall algorithm.
Define basic graph terminologies
A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E. Each edge is a
pair (v, w), where v, w V. Edges are sometimes referred to as arcs. Edges may have an
associated weight or cost.
If the pair is ordered, then the graph is directed. Directed graphs are sometimes referred
to as digraphs.
A path in a graph is a sequence of vertices w1, w2, w3, . . . , wn such that (wi, wi+1) E
for 1 i < n. The length of such a path is the number of edges on the path.
If the graph contains an edge (v, v) from a vertex to itself, then the path v, v is sometimes
referred to as a loop.
A simple path is a path such that all vertices are distinct, except that the rst and last
could be the same.
A cycle in a directed graph is a path of length at least 1 such that w1 = wn; this cycle is
simple if the path is simple.
A directed graph is acyclic if it has no cycles. It is also known as DAG.
An undirected graph is connected if there is a path from every vertex to every other
vertex. A directed graph with this property is called strongly connected. If a directed
graph is not strongly connected, but the underlying graph (without direction) is
connected, then the graph is said to be weakly connected.
A complete graph is a graph in which there is an edge between every pair of vertices.
Briefly explain the different ways of representing graphs with an example.
There are two ways of representing graphs namely adjacency matrix and adjacency list
In adjacency matrix, Graph is represented as a two-dimensional array.
o For each edge (u, v), set A[u][v] = 1, otherwise 0.
o If the graph is weighted, then set A[u][v] = w, otherwise .
o An adjacency matrix is appropriate, if the graph is dense, i.e., |E| = O(|V|2).
o If the graph is sparse, a better solution is an adjacency list representation.

In adjacency list, for each vertex, keep a list of all adjacent vertices.
o The space requirement is O(|E| + |V|).
o If the edges have weights, then it is also stored in the adjacency lists.

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.2

What is meant by graph traversal?


Graph traversal may be done either on a directed or undirected graph.
Goal is to methodically explore every vertex and every edge, starting from a root vertex.
Eventually a tree is built out of graph by choosing certain edges.
Two ways of traversing a graph are:
o Breadth First Search
o Depth First Search
Traversal enables to:
o Compute the connected components
o Compute a spanning forest
o Find a simple cycle
Explain Breadth First Search traversal of a graph using an example.
Breadth-first search explores the nodes of a graph in increasing distance away from some
starting vertex s.
1. Start vertex is named as level 0.
2. Explore vertices adjacent to start vertex as level 1, then their adjacent vertices as level
2 and so on.
3. Repeat step 2 until all vertices are visited.
Any vertex is said to be in one of the states: Not visited, Visited but not fully explored,
Visited and fully explored
BFS builds breadth-first tree, in which paths to root represent shortest paths in G
Running time of BFS algorithm is O(V+E)
Example

Undirected Graph

BFS traversal from vertex s

1) Initially vertex s is visited. It is marked as level 0.


Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II


2)
3)
4)
5)
6)
7)

5.3

Vertices r and w are adjacent to s. They are marked as level 1.


Vertex w is explored next. Vertices x and t adjacent to w are marked as level 2.
Vertex r is explored next. Its adjacent vertex v is marked as level 2.
Vertex t is explored next. Its adjacent vertex u is marked as level 3.
Vertex x is explored next. Its adjacent vertex y is marked as level 3.
The final output of BFS traversal on given graph is S W R T X V U Y

Pseudocode
BFS(graph G)
{
Queue Q;
while (G has an unvisited node x)
{
visit(x);
Enqueue(x, Q);
while (Q is not empty)
{
z = Dequeue(Q);
for all (unvisited neighbor y of z)
{
visit(y);
Enqueue(y, Q);
}
}
}
}

Explain Depth First Search traversal of a graph using an example.


A DFS traversal of a graph G is as follows
1. Select an unvisited node x, visit it, and treat as the current node
2. Find an unvisited neighbor of the current node, visit it, and make it the new current
node
3. If the current node has no unvisited neighbors, backtrack to the its parent, and make
that parent the new current node
4. Repeat steps 3 and 4 until no more nodes can be visited.
5. If there are still unvisited nodes, repeat from step 1.
DFS traversal on a graph results in a depth first-tree.
DFS on a graph takes O(V+E) time
Example

Undirected Graph, DFS from vertex A


Vijai Anand

DFS traversal
cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.4

1) DFS from vertex A is as follows. Initially vertex A is marked as visited.


2) Its adjacent vertex B is visited next.
3) Next vertex C is visited. It has two adjacent vertices D and E.
o The edge (C,A) is not considered since vertex A is already visited
4) Vertex D is visited next. Vertex D has no adjacent unvisited vertex.
o Hence it backtracked (shown as black arc) to vertex C.
5) Vertex E is visited next. No unvisited adjacent vertices.
6) It's backtracked from E to C, then C to B and finally back to vertex A.
7) The vertices are visited in the order A B C D E
Pseudocode
DFS(Graph G)
{
Stack S;
while (G has an unvisited node x)
{
visit(x);
push(x,S);
while (S is not empty)
{
t = peek(S);
if (t has an unvisited neighbor y)
{
visit(y);
push(y,S);
}
else
pop(S);
}
}
}

Explain Topological sort


A topological sort is an ordering of vertices in a directed acyclic graph, such that if there
is a path from vi to vj, then vj appears after vi in the ordering.
The indegree of a vertex v, is the number of edges (u, v).
A topological ordering is not possible if the graph has a cycle
The ordering is not necessarily unique; any legal ordering is possible.
Simple algorithm
1. The indegree for each vertex is stored, and that the graph is read into an adjacency list
2. Repeat until there are vertices remaining.
a. Find any vertex with no incoming edges, i.e., with indegree 0.
b. Print that vertex, and remove it, along with outgoing edges, from the graph.
In the search for a vertex of indegree 0, all the vertices are searched, even though only a
few have changed.
The efficiency of algorithm is O(|V|2 + |E|), i.e., quadratic running time.

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.5

Legal orderings
1) SSLC HSS Science Master
2) SSLC HSS Arts Master
3) SSLC HSS Engg Master
4) SSLC Diploma

Engg

Master

Improved Algorithm
After initial scanning, only those vertices whose updated indegrees have become zero are
scanned.
1. Store each vertexs In-Degree in an array
2. Initialize a queue with all in-degree zero vertices
3. While there are vertices remaining in the queue:
a. Dequeue and output a vertex
b. Reduce In-Degree of all vertices adjacent to it by 1
c. Enqueue any of these vertices whose In-Degree became zero
4. The topological ordering then is the order in which the vertices dequeue.

Acyclic graph

Topological Sort

The time to perform this algorithm is O(|E| + |V|)


Pseudocode
void topsort( )
{
Queue<Vertex> q;
int counter = 0;
q.makeEmpty( );
for each Vertex v
if( v.indegree == 0 )
q.enqueue( v );
while( !q.isEmpty( ) )
{

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II


Vertex v
v.topNum
for each
if(

= q.dequeue( );
= ++counter;
Vertex w adjacent to v
--w.indegree == 0 )
q.enqueue( w );

5.6

// Assign next number

}
}

What is minimum spanning tree? List its properties.


A minimum spanning tree (MST) is a subgraph for the given undirected graph.
A MST of an undirected graph G is a tree formed from graph edges that connects all the
vertices of G at lowest total cost. A MST exists if and only if G is connected.
The minimum spanning tree is a tree because it is acyclic, it is spanning because it covers
every vertex, and minimizes the cost.
An edge is added to MST, if it does not create a cycle and minimizes the total cost.
The two algorithms to find MST are Prim's and Kruskal algorithm.

Graph

MST

Explain Prim's algorithm with an example.


Prims algorithm is an MST algorithm that works much like Dijkstras algorithm does for
shortest path trees.
Algorithm
1. Pick some arbitrary start node s. Initialize tree T = {s}.
2. A new vertex to add to the tree by choosing the edge (u, v) such that the cost of (u, v) is
the smallest among all edges where u is in the tree and v is not.
3. Repeatedly add the shortest edge incident to T until the tree spans all the nodes.

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.7

For each vertex, maintian dv and pv to indicate whether it is known or unknown.


i.
dv is the weight of the shortest edge connecting v to a known vertex,
ii.
pv, is the last vertex to cause a change in dv.
After a vertex, v, is selected, for each unknown w adjacent to v, dw = min(dw, Cw,v).
The MST is built, starting with v1 selected as root.
i.
v1 is selected (known is changed from F to T). For adjacent vertices (v2, v3, v4)
dv and pv are updated.
ii.
The next vertex selected is v4, since it is minimum amongst unknown.
a. Every vertex is adjacent to v4 is examined, except v1 because it is known.
b. v2 is unchanged, because it has dv = 2 and the edge cost from v4 to v2 is 3
c. For vertices v3, v5, v6, v7 variables dv and pv are updated.
iii.
Next vertex chosen is v2 (arbitrarily breaking a tie). There is no change in table.
iv.
Then v3 is chosen, which results in updation to the distance in v6,
v.
Next v7 is chosen, which updates entries for v6 and v5.
vi.
v6 and then v5 are selected next in order and the algorithm terminates.

Initial configuration

after v1 is declared known

after v4 is declared known

after v2 and v3 is known

after v7 is declared known

after v6 and v5 is known

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.8

The edges in the spanning tree is read from the table: (v2, v1), (v3, v4), (v4, v1), (v5, v7),
(v6, v7), (v7, v4). The total cost is 16.
The running time is O(|E| log |V|)
Explain Kruskal's algorithm with an example.
Kruskals algorithm also adopts a greedy strategy to find MST.
Continually select the edges in order of smallest weight and accept an edge if it does not
cause a cycle.
Kruskals algorithm maintains a forest, i.e., a collection of trees. Initially, there are |V|
single-node trees.
Decide whether edge (u, v) should be accepted or rejected. Adding an edge merges two
trees into one.
The algorithm terminates when enough edges are accepted. Eventually there is only one
tree, i.e., minimum spanning tree.

Kruskal's MST

Edge Selection Status

The resultant minimum spanning tree decision pertaining to each edge is shown above.
The running time of this algorithm is O(|E| log |V|).

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.9

Pseudocode
vector<Edge> kruskal(vector<Edge> edges, int numVertices)
{
DisjSets ds{ numVertices };
priority_queue pq{ edges };
vector<Edge> mst;
while( mst.size() != numVertices - 1 )
{
Edge e = pq.pop( );
// Edge e = (u, v)
SetType uset = ds.find( e.getu());
SetType vset = ds.find( e.getv());
if( uset != vset )
{
mst.push_back(e);
// Accept the edge
ds.union(uset, vset);
}
}
return mst;
}

Compare Prim and Kruskal algorithm.


Both have the same output, i.e., minimum spanning tree
Kruskals begins with forest and merge into a tree
Prims always stays as a tree
If you dont know all the weight on edges use Prims algorithm
If you only need partial solution on the graph use Prims algorithm
Kruskals has better running times if the number of edges is low, whereas Prims has a
better running time if both the number of edges and the number of nodes are low.
The best algorithm depends on the graph and the cost of complex data structures.
Define shortest path algorithm
Given as input a weighted graph, G = (V, E), and a distinguished vertex, s, nd the
shortest weighted path from s to every other vertex in G.
The input is a weighted graph, i.e., for each edge (vi, vj), there is an associated cost Ci,j
to traverse the edge.
The cost of a path v1v2 . . . vn is sum of the cost with associated edges. This is referred to
as the weighted path length.
For unweighted graph, the path length is number of edges on the path, namely, N 1.
Using Dijkstras algorithm, find the shortest path from the source to all other nodes of the
graph 'G
Dijkstras algorithm is widely used to solve the single-source shortest-path problem.
It is a greedy algorithm that attempts to solve a problem in stages by doing what is the
best thing at that stage.
At each stage, Dijkstras algorithm selects a vertex, v, which has the smallest dv among
all the unknown vertices and declares that the shortest path from s to v is known.
For each unknown adjacent vertex w, dv is updated as dv + Cv,w if it results in a new
minimum. Then pv is set to v.
Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.10

Example
For each vertex, three pieces of information is tracked.
o Distance from source vertex s to vertex v is kept in dv.
o Entry in pv is the bookkeeping variable, that enables to print the actual paths.
o Entry known is set to true and the vertex is processed, i.e., dv and pv values for
adjacent vertices are updated.
The graph to be considered and the initial configuration with start node as v1.
o Initially all vertices are unreachable except for s, whose path length is 0.

The first vertex selected is v1, with path length 0 and is marked as known.
o Vertices adjacent to v1 are v2 and v4.
o pv value for these vertices is set to v1.
o dv value for v2 and v4 are updated to 2 and 4 respectively

Next, v4 is selected, since it has minimum dv amongst unknown vertices and is marked
as known. Entries for adjacent vertices v3, v5, v6, and v7 are updated.

Next, v2 is selected.
o v4 is adjacent but already known, hence no changes are made.
Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.11

o v5 is adjacent but not adjusted, since the cost of going through v2 is 2 + 10 = 12


against the existing path of length 3 is already known.

Next vertex selected is v5 at cost 3. v7 is the only adjacent vertex, but it is not updated,
because 3 + 6 > 5.
Next v3 is selected, and the distance for v6 is adjusted to 3 + 5 = 8

Next, v7 is selected; v6 gets updated down to 5 + 1 = 6.

Finally, v6 is selected and the algorithm terminates as all vertices are known.

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.12

The running time for Disjkstra's algorithm is O(|E| log |V|)


Algorithm
void dijkstra( Vertex s )
{
for each Vertex v
{
v.dist = INFINITY;
v.known = false;
}
s.dist = 0;
while( there is an unknown distance vertex )
{
Vertex v = smallest unknown distance vertex;
v.known = true;
for each Vertex w adjacent to v
if( !w.known )
{
DistType cvw = cost of edge from v to w;
if( v.dist + cvw < w.dist )
{
decrease( w.dist to v.dist + cvw );
w.path = v;
}
}
}
}

// Update w

Explain Bellman-Ford algorithm with an example.


Dijkstra's algorithm does not work for graph whose edges have negative cost.
Bellman-Ford algorithm is used for directed graphs with edges having negative weight.
It is also used to determine if there exists a negative-weight cycle
For example, currency exchange in which, start with one currency and end up after a
sequence of conversions with more money than what was started with.
Algorithm
Iterate over the number of vertices
Keep track of current shortest path (distance and parent) for each vertex
For each iteration, relax all edges
Consider whether this edge can be used to improve the current shortest path of the vertex
at its endpoint.
After k iterations, the first k steps of any shortest path are correct and will never change
from that point
Because every shortest path is simple, we know that after at most n-1 iterations, every
shortest path is determined
Complexity of Bellman-Ford algorithm is O(|E| |V|).
If after an iteration in which nothing changes (no vertices receive improved shortest
paths) the algorithm is finished nothing can change from that point.

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.13

To check for cycles, after completing n-1 iterations, simply scan all edges one more time
to see if there is a vertex that could still be improved.
Example

Graph with negative edges

Bellman-Ford solution

Initial configuration:
S
d[v]

-2

-2

p[v]
Iteration 1:

d[v]
p[v]
Iteration 2:

d[v]
p[v]
Iteration 3:

d[v]
p[v]

Since, there is no change in distance after iteration 2, the algorithm terminates.


Since the algorithm stabilizes after few iteration, there is no negative cycle in the graph.
Shortest path from vertex S to T is S
A C B T

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.14

Pseudocode
Bellman-Ford (G,w,s)
{
For every vertex v
d[v] = INFINITY
d[s] = 0
For i=1 to |V|-1 do
{
For every edge (u,v) in E do
{
If d[v]>d[u]+w(u,v) then
{
d[v]=d[u]+w(u,v)
parent[v] = u
}
}
}
For every edge (u,v) in E do
If d[v]>d[u]+w(u,v) then
Return NEGATIVE CYCLE
Return d[], parent[]
}

// Relaxation

Illustrate Floyd-Warshall algorithm with an example.


Floyd-Warshall Algorithm finds shortest paths between all pairs of vertices in a graph.
A common example in the real world is a road map.
Algorithm
Let the vertices in a graph be numbered from 1 n
Shortest path from vertex i to vertex j uses vertices in the subset {1, 2,,k}. The two
scenarios are:
o k is an intermediate vertex on the shortest path
o k is not an intermediate vertex on the shortest path

Algorithm
Initialize distance matrix D as adjacency matrix of the given weighted graph
Initialize path matrix P as p[i][j] = j if vertex j is adjacent to vertex i.
At each step, find out whether or not using vertex k will improve an estimate between the
distances between vertex i and vertex j. If it improves
o record the new shortest path weight between i and j
o record the fact that the shortest path between i and j goes through k
Final D matrix contains all the shortest paths and final P matrix is used to reconstruct the
shortest path between any pair of vertices.

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.15

Example
0

Nil

Nil

Nil

Nil

6
Weighted Graph

Distance matrix

Path matrix

Consider vertex 1. Update min(D[i,j], D[i,k]+D[k,j]) for non-shaded regions.


o D(3,2) = D(3,1) + D(1,2) = 10 and P(3,2) = 1
0

Nil

Nil

10

Nil

6
Vertex 1

Updated Distance matrix

Updated Path matrix

Consider vertex 2. Update min(D[i,j], D[i,k]+D[k,j]) for non-shaded regions.


o D(1,3) = D(1,2) + D(2,3) = 6 and P(1,3) = 2
0

Nil

Nil

10

10

Nil

Vertex 2

Updated Distance matrix

Updated Path matrix

Consider vertex 3. There is no change.


0

Nil

Nil

10

10

Nil

Vertex 3

Final Distance matrix

Final Path matrix

For example path[1][3] = 2. This implies 2 is the vertex last visited before vertex 3. Thus
the path from vertex 1 to 3 is 1 2 3. The shortest distance from vertex 1 to 3 is 6.

Vijai Anand

cseannauniv.blogspot.in

CS6301-Prog. & Data Structures II

5.16

Pseudocode
For k=1 to n
{
For i=1 to n
{
For j=1 to n
{
if (D[i][k]+D[k][j] < D[i][j])
{
D[i][j] = D[i][k]+D[k][j];
path[i][j] = path[k][j];
}
}
}
}

Differentiate between DFS and BFS.


BFS
Backtracking is not possible
Vertices explored are organized in a FIFO
queue
Vertices at the same level are searched
simultaneously

DFS
Backtracking is possible from a dead end
Vertices are processed in a LIFO order
Search is done in one particular direction at a
time until dead end is reached

Examples for graph terminologies

Undirected graph

Directed & weighted graph

Directed graph cycle

Undirected graph cycle

Weakly connected Digraph

Strongly connected Digraph

Vijai Anand

cseannauniv.blogspot.in

You might also like