480 likes | 584 Views
Graph. A graph G=(V,E) consists of a set of vertices V and set of edges E. Vertices are referred as nodes and the arc between the nodes are referred as Edges. Vertices = { A,B,C,D } Edges = {(A,B),(A,C),(A,D),(B,C),(B,D),(C,D)}. A. B. C. D. Basic terminologies.
E N D
Graph A graph G=(V,E) consists of a set of vertices V and set of edges E. Vertices are referred as nodes and the arc between the nodes are referred as Edges. Vertices = { A,B,C,D } Edges = {(A,B),(A,C),(A,D),(B,C),(B,D),(C,D)} A B C D
Basic terminologies • Directed Graph (or) Digraph - directed edges - unidirectional - if (v,w) is a directed graph then (v,w)≠(w,v) • Undirected Graph - undirected edges - bidirectional - if (v,w) is a undirected graph then (v,w)=(w,v)
Weighted Graph - every edge is assigned a weight or value - it can be directed or undirected • Complete Graph - an edge between every pair of vertices - if n vertices then n(n-1)/2 edges - a path from every vertex to every other vertex 2 1 4 1 2 3
Strongly Connected Graph - a path from every vertex to every other vertex in a directed graph - otherwise it is weakly connected • Path - sequence of vertices - path from A to C is A,B,C A C B Strongly connected Weakly connected
Cyclic Graph and Acyclic Graph - cyclic : path in which first and last vertex are the same - Acyclic : no cycles • Length - no. of edges on the path ( length of the path from B to D is 2 ) A B D C Cyclic Acyclic
Symmetric graph - every pair of vertices can have both way path • Subgraph - a subgraph of a graph G=(V,E) is also a graph G’=(V’,E’) such that V’ V and E’ E 1 1 1 3 3 2 3 2 2 4 4
Degree - no. of edges incident on the vertex determines its degree. - indegree: no. of edges entering into the vertex - outdegree: no. of edges exiting from the vertex indegree(V1) = 2 indegree(V3) = 1 outdegree(V2) = 0 outdegree(V4) = 2 • Loop - an edge (V1,V1) from a vertex to itself V2 V! V3 V4
Graph representation A B C D A B C D A A 0 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 1 1 1 0 1 0 1 1 0 A B A B • Adjacency matrix representation B B C C C D C D D D A B C D A B C D 2 4 A 0 4 2 3 ∞ 0 ∞∞ ∞ ∞ 0 2 ∞ 1 ∞ 0 A B A B A 0 2 1 ∞ 2 0 3 4 1 3 0 2 ∞ 4 2 0 B 3 B 3 2 1 4 1 C C C D C D D D 2 2
B B C D A C A B A B A B A C D • Adjacency list representation B C A D B C C D C D D D B C B D 4 2 A B B 4 C 2 D 3 A B B 2 C 1 A A 3 3 2 B A 2 C 3 D 4 1 1 4 B C D C D C 1 D 1 A 1 B 3 D 2 2 C B 1 D B 4 C 2 D
Topological Sort (or) Ordering • Linear ordering of vertices in DAG such that if there is a path from Vi to Vj , then Vj appears after Vi in the ordering. (if graph is cycle then this method is not possible) Arts HSS SSLC Science Master Dip. Engg.
Find the indegree for every vertex • Place the vertices whose indegree is 0 on the empty queue • Dequeue the vertex V and decrement the indegree’s of all its adjacent vertices • Enqueue the vertex on the queue, if its indegree falls to zero • Repeat the above three steps until the queue becomes empty • The topological ordering is the order in which the vertices dequeued
A B C D A 0 1 1 0 0 0 1 1 0 0 0 0 0 0 1 0 A C B C • indegree[A]=0 indegree[B]=1 indegree[C]=3 indegree[D]=1 • Enqueue the vertex whose indegree is 0 • Dequeue the vertex A from the queue and decrement the indegree’s of its adjacent vertex B and C indegree[B]=0 indegree[C]=2 enqueue the vertex B as its indegree falls to 0 • Dequeue B indegree[C]=1 indegree[D]=0 enqueue D B D D A B D
C • Dequeue D indegree[C]=0 enqueue C • Dequeue C ( no adjacency and empty queue ) 1 2 3 4 A 0 0 0 0 B 1 0 0 0 C 3 2 1 0 D 1 1 0 0 enqueue A B D C dequeue A B D C
A B C D A 0 1 1 0 0 0 1 1 0 0 0 0 0 0 1 0 B • Column contains leftmost zeros (mark the particular column and its corresponding row) • With the remaining matrix to do the above step • Arrange according to the order L • If it is upper triangular matrix then the order A,B,D,C C D L=1 2 4 3 A B D C A 0 1 0 1 0 0 1 1 0 0 0 1 0 0 0 0 B D C
void Topsort (Graph G); { Queue Q; int Counter = 0; Vertex V, W; Q = CreateQueue( NumVertex ); MakeEmpty (Q ); for each vertex V If(Indegree[ V ] == 0 ) Enqueue ( V, Q ); while (!IsEmpty( Q ) ) { V = Dequeue( Q ) ) TopNum[ V ] = ++Counter; /*Assign next number */ for each Q adjacent to V If( --Indegree[ W ] == 0 ) Enqueue(W, Q ); } If ( Counter != NumVerterx ) Error ( “Graph has a cycle” ); DisposeQueue( Q ); /*Free the memory) }
Graph Traversal Visiting the nodes in a specific order • Depth first traversal/search • Breadth first traversal/search - any node in the graph, designate it as the search node and mark it as visited - using the adjacency matrix, find all unvisited adjacent nodes to the search node and enqueue them - then the node is dequeued. Mark that node as visited and designate it as new search node - repeat step 2 and 3 using new search node - continues until the queue becomes empty
A B C D E A 0 1 0 1 1 1 0 1 1 0 0 1 0 1 1 1 1 1 0 0 1 0 1 0 0 A B B D E C D - Let A be the source vertex. Mark it as visited. Find the unvisited adjacent vertices of A and enqueue them B is dequeued, mark as visited and enqueue its adjacents D is dequeued, mark as visited and enqueue its adjacents E is dequeued, mark as visited and enqueue its adjacents E C B D E D E C E C C
Void BFS(vertex V) { visited[V]=1 enqueue(V,Q); while(!isempty(Q)) { V=dequeue(Q); print V; for all vertices W adjacent to V if(visited[W]==0) { enqueue(W,Q); visited[W]=1; } } } A C is dequeued : no adjacent and queue is empty B D E C
Depth First Search • Choose any node in the graph. make it as a search node and mark it as visited. • Find a node adjacent to search node that has not been visited yet. Make it as a new search node and mark it as visited. • Repeat step 2 using the new search node. If no nodes satisfying (2) can be found, return to the previous search node and continue from there. • If the graph still contains unvisited nodes, choose any node that has not been visited and repeat the above steps.
A Void DFS(Vertex V) { visited[V]=True for each W adjacent to V if(!visited[W]) DFS(W); } B C E D
A BFS A B D E B D E C C A B C D E A 0 1 0 1 1 1 0 1 1 0 0 1 0 1 1 1 1 1 0 0 1 0 1 0 0 DFS A B B C D C E E D
Shortest path algorithm The shortest path algorithm determines the minimum cost of the path from source to every other vertex Single source shortest path problem All pairs shortest path problem Single source shortest path problem Given an input graph G=(V,E) and a distinguished vertex S , find the shortest path from S to every other vertex in G. - unweighted shortest path - weighted shortest path
Unweighted Shortest Path known : specifies whether the vertex is processed or not. It is set to 1 after it is processed, otherwise 0. initially all vertices are marked as unknown ( 0 ) dv: specifies the distance from the source S, initially all vertices are unreachable except for S, whose path length is 0. pv : specifies the bookkeeping variable which will allow us to print. (the actual path)
Steps to perform unweighted shortest path • Assign the source node as S and enqueue S • Dequeue the vertex S and assign the value of that vertex to be known and find its adjacency vertices. • If the distance of the adjacent vertices is equal to infinity then change the distance of that vertex as the distance of its source vertex increment by 1 and enqueue the vertex. • Repeat the above 2 steps until the queue becomes empty
V known dv pv a 0 0 0 b 0 ∞ 0 c 0 ∞ 0 d 0 ∞ 0 enqueue a a b c d dequeue a V known dv pv a 1 0 0 b 0 1 a c 0 1 a d 0 ∞ 0 enqueue b, c dequeue c V known dv pv a 1 0 0 b 1 1 a c 1 1 a d 0 2 c enqueue d Shortest path from source a to other vertices a to b is 1 a to c is 1 a to d is 2 dequeue b V known dv pv a 1 0 0 b 1 1 a c 0 1 a d 0 1 b enqueue c,,d dequeue d V known dv pv a 1 0 0 b 1 1 a c 1 1 a d 1 2 c enqueue empty
Void unweighted(Table T) { Queue Q; Vertex V,W; Q=createQueue( Numvertex); makeempty(Q); enqueue(S,Q); while(!isempty(Q)) { V=dequeue(Q); T[V].known=TRUE; for each W adjacent to V if(T[W].Dist=INFINITY) { T[W].Dist=T[V].Dist+1; T[W].path=V; enqueue(W,Q); } } disposeQueue(Q); }
V1 V2 V3 V4 V5 V6 V7 V3 V1 1 V3 V2 2 V3 V4 2 V3 V5 3 V3 V6 1 V3 V7 3
Weighted Shortest Path(Dijkstra’s Algorithm) V1 V2 2 V1 V4 1 V1 V6 6 V1 V3 3 V1 V5 3 V1 V7 5 2 V1 V2 10 4 3 1 2 2 V3 V4 V5 4 5 8 6 V6 1 V7
d v pv d v pv d v pv v known v known v known v1 0 0 0 v1 1 0 0 v1 1 0 0 v2 0 ∞ 0 v2 0 2 v1 v2 0 2 v1 v3 0 ∞ 0 v3 0 ∞ 0 v3 0 3 v4 0 0 1 v4 ∞ 0 v4 1 v1 v4 1 v1 v5 0 ∞ 0 v5 0 ∞ 0 v5 0 3 v4 v6 0 ∞ 0 v6 0 ∞ 0 v6 0 9 v4 v7 0 ∞ 0 v7 0 ∞ 0 v7 0 5 v4 initial V1 turned as known V4 turned as known d v pv d v pv d v pv v known v known v known v1 1 0 0 v1 1 0 0 v1 1 0 0 v2 1 2 v1 v2 1 2 v1 v2 1 2 v1 v3 0 3 v4 v3 0 3 v4 v3 1 3 v4 1 1 1 v4 1 v1 v4 1 v1 v4 1 v1 v5 0 3 v4 v5 1 3 v4 v5 1 3 v4 v6 0 9 v4 v6 0 9 v4 v6 0 8 v3 v7 0 5 v4 v7 0 5 v4 v7 0 5 v4 V5 turned as known V3 turned as known V2 turned as known
d v pv d v pv v known v known 2 v1 1 0 0 v1 1 0 0 3 v2 1 2 v1 v2 1 2 v1 1 v3 1 3 v4 v3 1 3 v4 • V1 V2 • V1 V3 • V1 V4 • V1 V5 • V1 V6 • V1 V7 1 1 3 v4 1 v1 v4 1 v1 v5 1 3 v4 v5 1 3 v4 6 v6 0 6 v7 v6 1 6 v7 5 v7 1 5 v4 v7 1 5 v4 V7 turned as known V6 turned as known
Declaration typedef int Vertex; struct TableEntry { List header; int known; Disttype Dist; Vertex Path; } #define NotAVertex (-1) typedef struct TableEntry Table[NumVertex]; Initialization void initTable(Vertex Start, Graph G, Table T) { T[i].known=FALSE; T[i].Dist=INFINITY; T[i].Path=NotAVertex; } T[Start].Dist=0; }
Void WeightedPath(Table T) { Vertex V,W; for( ; ;) { V=smallest unknown distance vertex; if(V=NotAVertex) break; T[V].known=TRUE; for each W adjacent to V if(!T[W].known) if(T[V].Dist+Cvw < T[W].Dist) { Decrease(T[W].Dist to T[V].Dist+Cvw); T[W].Path=V; } } }
Minimum Spanning Tree MST of an undirected graph G is a tree formed from graph edges that connects all vertices of G at lowest cost It is an acyclic Spanning: it covers every vertex Minimum: it covers with minimum cost Types – 1. Prim’s Algorithm 2. Kruskal’s Algorithm 2 V1 V2 10 4 3 1 2 7 V3 V4 V5 4 5 8 6 V6 1 V7 2 V1 V2 V1 V2 1 1 V3 V4 V5 V3 V4 V5 V6 V7 V6 V7
2 2 V1 V2 V1 V2 1 1 2 V3 V4 V5 V3 V4 V5 V6 V7 V6 V7 2 2 V1 V2 V1 V2 1 1 2 2 V3 V4 V5 V3 V4 V5 4 4 1 V6 V7 V6 V7
2 2 V1 V2 V1 V2 1 1 2 2 V3 V4 V5 V3 V4 V5 4 4 6 1 1 V6 V7 V6 V7
d v pv d v pv d v pv v known v known v known v1 0 0 0 v1 1 0 0 v1 1 0 0 v2 0 ∞ 0 v2 0 2 v1 v2 0 2 v1 v3 0 ∞ 0 v3 0 4 v1 v3 0 2 v4 0 0 1 v4 ∞ 0 v4 1 v1 v4 1 v1 v5 0 ∞ 0 v5 0 ∞ 0 v5 0 7 v4 v6 0 ∞ 0 v6 0 ∞ 0 v6 0 8 v4 v7 0 ∞ 0 v7 0 ∞ 0 v7 0 4 v4 initial V1 turned as known V4 turned as known d v pv d v pv d v pv v known v known v known v1 1 0 0 v1 1 0 0 v1 1 0 0 v2 1 2 v1 v2 1 2 v1 v2 1 2 v1 v3 0 2 v4 v3 1 2 v4 v3 1 2 v4 1 1 1 v4 1 v1 v4 1 v1 v4 1 v1 v5 0 7 v4 v5 0 7 v4 v5 0 6 v7 v6 0 8 v4 v6 0 5 v3 v6 0 1 v7 v7 0 4 v4 v7 0 4 v4 v7 1 4 v4 V3 turned as known V7 turned as known V2 turned as known
d v pv d v pv v known v known v1 1 0 0 v1 1 0 0 v2 1 2 v1 v2 1 2 v1 v3 1 2 v4 v3 1 2 v4 1 1 v4 1 v1 v4 1 v1 v5 0 6 v7 v5 1 6 v7 v6 1 1 v7 v6 1 1 v7 v7 1 4 v4 v7 1 4 v4 V6 turned as known V5 turned as known 2 2 V1 V2 V1 V2 10 4 3 1 1 2 7 2 V3 V4 V5 V3 V4 V5 4 4 6 5 8 6 1 V6 1 V7 V6 V7
Kruskal’s Algorithm 2 V1 V2 10 4 3 1 2 7 V3 V4 V5 4 5 8 6 V6 1 V7 V1 V2 V1 V2 1 1 V3 V4 V5 V3 V4 V5 1 V6 V7 V6 V7 (V1,V4) (V6,V7)
2 2 V1 V2 V1 V2 1 1 2 V3 V4 V5 V3 V4 V5 1 1 V6 V7 V6 V7 (V3,V4) (V1,V2) 2 2 V1 V2 V1 V2 3 4 1 1 2 2 V3 V4 V5 V3 V4 V5 1 1 V6 V7 V6 V7 (V2,V4) (V1,V3)
2 2 V1 V2 V1 V2 1 1 2 2 V3 V4 V5 V3 V4 V5 4 4 5 1 1 V6 V7 V6 V7 (V4,V7) (V3,V6) 2 V1 V2 1 2 V3 V4 V5 4 6 1 V6 V7 (V5,V7)
1 2 V1 V2 10 4 3 1 2 7 V3 V4 V5 4 5 8 6 V6 1 V7 2 V1 V2 4 3 2 V3 V4 V5 4 5 6 V6 1 V7
Void Kruskal(Graph G) { int EdgesAccepted=0; DisjSet S; Heap H; Edge E; Initialize(s); BuildHeap(H) while(EdgesAccepted < NumVertex-1) { E=DeleteMin(H); Uset=Find(U,S); Vset=Find(V,S); if(Uset != Vset) { EdgesAccepted++; SetUnion(S,Uset,Vset); } } }
Biconnectivity A connected undirected graph is biconnected if there are no vertices whose removal disconnects the rest of the graph. Articulation Point The vertices whose removal would disconnect the graph are known as articulation point B A B A C D C D F F G E G E
B A B A C D C D F F G E G E B A B A B A C D C D C D F F F G E G E G E
DFS with Num and Low (post order traversal) Low(F)=Min( Num(F), Num(D) ) Min( 6,4 ) = 4 //no tree edge & only back edge Low(E)=Min( Num(E), Low(F) ) Min( 5,4 ) = 4 // no back edge Low(D)=Min( Num(D), Low(E), Num(A) ) Min( 4,4,1) = 1 Low(G)=Min( Num(G) ) = 7 //no tree edge & back edge Low(C)=Min( Num(C), Low(D), Low(G) ) Min( 3,1,7 ) = 1 Low(B)=Min( Num(B), Low(C) ) Min( 2,1 ) = 1 Low(A)=Min( Num(A), Low(B) ) Min( 1,1 ) = 1 Low(G)>=Num(C) 7 > 3 ie ( if Low(W) >= Num(V) then Low(E)>=Num(D) 4 = 4 V is an articulation point ) A (1/1) B (2/1) C (3/1) D G (4/1) (7/7) E (5/4) F (6/4) A B C D E F G A 0 1 0 1 0 0 0 B 1 0 1 0 0 0 0 C 0 1 0 1 0 0 1 D 1 0 1 0 1 1 0 E 0 0 0 1 0 1 0 F 0 0 0 1 1 0 0 G 0 0 1 0 0 0 0
Euler’s Circuit - each vertex has an even degree - graph should be connected and cycle - visit each edge only once - visit each vertex one or more time C C A F Start from C, then go C, A, F, C A F D D B G B G E E
C Now back to F and process F, D, G, F The path C AFC and F D G F C A F D G F C A F D B G E C Then from A, process A D B A The path C A F D G F C and A D B A C A D B A F D G F C A F D B G E
C Then from B, process B G E B The path C A D B A F D G F C and B G E B C A D B G E B A F D G F C A F D B G C A D B G E B A F D G F C E CA B G B A A F F D D G G F F C AD D B G E E B C C A F A F D D B G B G E E