Anda di halaman 1dari 18

Smartzworld.com Smartworld.

asia

Lecture Notes

UNIT V - GRAPHS
Representation of Graphs – Breadth-first search – Depth-first search – Topological sort –
Minimum Spanning Trees – Kruskal and Prim algorithm – Shortest path algorithm –
Dijkstra’s algorithm – Bellman-Ford algorithm – Floyd - Warshall algorithm.

Define basic graph terminologies


o 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.
o If the pair is ordered, then the graph is directed. Directed graphs are sometimes referred
to as digraphs.
o 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.
o 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.
o A simple path is a path such that all vertices are distinct, except that the ¿rst and last
could be the same.
o 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.
o A directed graph is acyclic if it has no cycles. It is also known as DAG.
o 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.
o 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.


o There are two ways of representing graphs namely adjacency matrix and adjacency list
o 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.

o 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.

CS6301 Programming & Data Structures II Unit V Page 1

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

What is meant by graph traversal?


o Graph traversal may be done either on a directed or undirected graph.
o Goal is to methodically explore every vertex and every edge, starting from a root vertex.
o Eventually a tree is built out of graph by choosing certain edges.
o Two ways of traversing a graph are:
o Breadth First Search
o Depth First Search
o 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.


o 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.
o Any vertex is said to be in one of the states: Not visited, Visited but not fully explored,
Visited and fully explored
o BFS builds breadth-first tree, in which paths to root represent shortest paths in G
o Running time of BFS algorithm is O(V+E)
Example

CS6301 Programming & Data Structures II Unit V Page 2

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

Undirected Graph BFS traversal from vertex s


1) Initially vertex s is visited. It is marked as level 0.
2) Vertices r and w are adjacent to s. They are marked as level 1.
3) Vertex w is explored next. Vertices x and t adjacent to w are marked as level 2.
4) Vertex r is explored next. Its adjacent vertex v is marked as level 2.
5) Vertex t is explored next. Its adjacent vertex u is marked as level 3.
6) Vertex x is explored next. Its adjacent vertex y is marked as level 3.
7) 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.


o 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.
o DFS traversal on a graph results in a depth first-tree.
o DFS on a graph takes O(V+E) time

CS6301 Programming & Data Structures II Unit V Page 3

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

Example

Undirected Graph, DFS from vertex A DFS traversal

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);
}
}
}

CS6301 Programming & Data Structures II Unit V Page 4

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

Explain Topological sort


o 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.
o The indegree of a vertex v, is the number of edges (u, v).
o A topological ordering is not possible if the graph has a cycle
o 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.
o In the search for a vertex of indegree 0, all the vertices are searched, even though only a
few have changed.
o The efficiency of algorithm is O(|V|2 + |E|), i.e., quadratic running time.

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

Improved Algorithm
o After initial scanning, only those vertices whose updated indegrees have become zero are
scanned.
1. Store each vertex’s 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.

CS6301 Programming & Data Structures II Unit V Page 5

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

Acyclic graph Topological Sort


o 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( ) )
{
Vertex v = q.dequeue( );
v.topNum = ++counter; // Assign next number
for each Vertex w adjacent to v
if( --w.indegree == 0 )
q.enqueue( w );
}
}

What is minimum spanning tree? List its properties.


o A minimum spanning tree (MST) is a subgraph for the given undirected graph.
o 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.
o The minimum spanning tree is a tree because it is acyclic, it is spanning because it covers
every vertex, and minimizes the cost.
o An edge is added to MST, if it does not create a cycle and minimizes the total cost.
o The two algorithms to find MST are Prim's and Kruskal algorithm.

CS6301 Programming & Data Structures II Unit V Page 6

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

Graph MST

Explain Prim's algorithm with an example.


o Prim’s algorithm is an MST algorithm that works much like Dijkstra’s 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.

o 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.
o After a vertex, v, is selected, for each unknown w adjacent to v, dw = min(dw, Cw,v).
o 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

CS6301 Programming & Data Structures II Unit V Page 7

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

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

CS6301 Programming & Data Structures II Unit V Page 8

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

o 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.
o The running time is O(|E| log |V|)

Explain Kruskal's algorithm with an example.


o Kruskal’s algorithm also adopts a greedy strategy to find MST.
o Continually select the edges in order of smallest weight and accept an edge if it does
not cause a cycle.
o Kruskal’s algorithm maintains a forest, i.e., a collection of trees. Initially, there are |V|
single-node trees.
o Decide whether edge (u, v) should be accepted or rejected. Adding an edge merges
two trees into one.
o 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

CS6301 Programming & Data Structures II Unit V Page 9

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

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

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.


o Both have the same output, i.e., minimum spanning tree
o Kruskal’s begins with forest and merge into a tree
o Prim’s always stays as a tree
o If you don’t know all the weight on edges use Prim’s algorithm
o If you only need partial solution on the graph use Prim’s algorithm
o Kruskal’s has better running times if the number of edges is low, whereas Prim’s has a
better running time if both the number of edges and the number of nodes are low.
o The best algorithm depends on the graph and the cost of complex data structures.

Define shortest path algorithm


o 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.
o The input is a weighted graph, i.e., for each edge (vi, vj), there is an associated cost Ci,j
to traverse the edge.
o The cost of a path v1v2 . . . vn is sum of the cost with associated edges. This is referred to
as the weighted path length.
o For unweighted graph, the path length is number of edges on the path, namely, N í 1.

Using Dijkstra’s algorithm, find the shortest path from the source to all other nodes of
the graph 'G’
o Dijkstra’s algorithm is widely used to solve the single-source shortest-path problem.
o It is a greedy algorithm that attempts to solve a problem in stages by doing what is the

CS6301 Programming & Data Structures II Unit V Page 10

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

best thing at that stage.


o At each stage, Dijkstra’s 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.
o 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.

Example
o 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.
o 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.

o 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

o 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.

CS6301 Programming & Data Structures II Unit V Page 11

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

o Next, v2 is selected.
o v4 is adjacent but already known, hence no changes are made.
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.

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

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

CS6301 Programming & Data Structures II Unit V Page 12

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

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

o 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 ); // Update w
w.path = v;
}
}
}
}
CS6301 Programming & Data Structures II Unit V Page 13

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

Explain Bellman-Ford algorithm with an example.


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

o 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


o Initial configuration:
S A B C T
d[v] 0 ’’’’
p[v]

CS6301 Programming & Data Structures II Unit V Page 14

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

o Iteration 1:
S A B C T
d[v] 0 7 6 4 2
p[v] S S A B
o Iteration 2:
S A B C T
d[v] 0 7 2 4 -2
p[v] S C A B
o Iteration 3:
S A B C T
d[v] 0 7 2 4 -2
p[v] S C A B
o Since, there is no change in distance after iteration 2, the algorithm terminates.
o Since the algorithm stabilizes after few iteration, there is no negative cycle in the graph.
o Shortest path from vertex S to T is S Æ A Æ C Æ B Æ T

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 // Relaxation
{
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[]
}

CS6301 Programming & Data Structures II Unit V Page 15

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

Illustrate Floyd-Warshall algorithm with an example.


o Floyd-Warshall Algorithm finds shortest paths between all pairs of vertices in a graph.
o A common example in the real world is a road map.
Algorithm
o Let the vertices in a graph be numbered from 1 … n
o 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
o Initialize distance matrix D as adjacency matrix of the given weighted graph
o Initialize path matrix P as p[i][j] = j if vertex j is adjacent to vertex i.
o 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
o Final D matrix contains all the shortest paths and final P matrix is used to reconstruct the
shortest path between any pair of vertices.

Example

0 4 7 Nil 2 3
1 0 2 1 Nil 3
6 ’0 1 Nil Nil

Weighted Graph Distance matrix Path matrix

CS6301 Programming & Data Structures II Unit V Page 16

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

o 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 4 7 0 4 7 Nil 2 3
1 0 2 1 0 2 1 Nil 3
6 ’0 6 10 0 1 1 Nil

Vertex 1 Updated Distance matrix Updated Path matrix

o 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 4 7 0 4 6 Nil 2 2
1 0 2 1 0 2 1 Nil 3
6 10 0 6 10 0 1 1 Nil

Vertex 2 Updated Distance matrix Updated Path matrix

o Consider vertex 3. There is no change.

0 4 6 0 4 6 Nil 2 2
1 0 2 1 0 2 1 Nil 3
6 10 0 6 10 0 1 1 Nil

Vertex 3 Final Distance matrix Final Path matrix

o 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.

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];
CS6301 Programming & Data Structures II Unit V Page 17

jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia

Lecture Notes

path[i][j] = path[k][j];
}
}
}
}

Differentiate between DFS and BFS.


BFS DFS
Backtracking is not possible Backtracking is possible from a dead end
Vertices explored are organized in a FIFO Vertices are processed in a LIFO order
queue
Vertices at the same level are searched Search is done in one particular direction at a
simultaneously 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

CS6301 Programming & Data Structures II Unit V Page 18

jntuworldupdates.org Specworld.in

Anda mungkin juga menyukai