Professional Documents
Culture Documents
5.1
UNIT V
GRAPHS
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
5.2
Undirected Graph
cseannauniv.blogspot.in
5.3
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);
}
}
}
}
DFS traversal
cseannauniv.blogspot.in
5.4
Vijai Anand
cseannauniv.blogspot.in
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
Vijai Anand
cseannauniv.blogspot.in
= q.dequeue( );
= ++counter;
Vertex w adjacent to v
--w.indegree == 0 )
q.enqueue( w );
5.6
}
}
Graph
MST
Vijai Anand
cseannauniv.blogspot.in
5.7
Initial configuration
Vijai Anand
cseannauniv.blogspot.in
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
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
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;
}
cseannauniv.blogspot.in
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
5.11
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
Finally, v6 is selected and the algorithm terminates as all vertices are known.
Vijai Anand
cseannauniv.blogspot.in
5.12
// Update w
Vijai Anand
cseannauniv.blogspot.in
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
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]
Vijai Anand
cseannauniv.blogspot.in
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
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
5.15
Example
0
Nil
Nil
Nil
Nil
6
Weighted Graph
Distance matrix
Path matrix
Nil
Nil
10
Nil
6
Vertex 1
Nil
Nil
10
10
Nil
Vertex 2
Nil
Nil
10
10
Nil
Vertex 3
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
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];
}
}
}
}
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
Undirected graph
Vijai Anand
cseannauniv.blogspot.in