Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor Neelima Gupta Table of Contents Graph Algorithms Thanks to: Sunaina Kalucha (29) (MCS '11)

Similar presentations


Presentation on theme: "Instructor Neelima Gupta Table of Contents Graph Algorithms Thanks to: Sunaina Kalucha (29) (MCS '11)"— Presentation transcript:

1 Instructor Neelima Gupta ngupta@cs.du.ac.in

2 Table of Contents Graph Algorithms Thanks to: Sunaina Kalucha (29) (MCS '11)

3 DEPTH FIRST SEARCH (DFS) Given a graph G = (V,E), visit all the vertices in a depth first order. Informal Algo: Start at an arbitrary vertex s. Visit the first vertex (arbitrarily chosen) adjacent to s, if it has not been visited earlier and recurse else backtrack and repeat with other adjacent vertices of s. If there are more unvisited vertices repeat the above until there are no more unvisited vertices. Informal Anlaysis: Each vertex is visited at least once and each edge is scanned exactly twice, thus O(V+E)

4 Formal Analysis of DFS Operation PUSH POP # of times operation is executed by DFS Θ(max(V,E)) Total time each operation takes Θ (1) Total time taken by DFS Θ (E+V)  Given a graph G with vertices V and edges E.  Data Structure used : STACK TOTAL= Θ (E+V) Thanks to: Sunaina Kalucha (29 ) (MCS '11) No. of pop operations is equal to the no of push as algo will empty the stack. For both disconnected or connected graph, we push the neighbours of each encountered vertex(i.e. edges) and this is done for every vertex.

5 BREATH FIRST SEARCH (BFS) Given a graph G = (V,E) and a vertex s, visit all the vertices reachable from s in a breath first order. Informal Algo: Startin from vertex s, visit all the vertices adjacent to s in turn, if it has not been visited earlier, then visit their neighbours and so on. Informal Anlaysis: Since in the beginning we do not know which vertex is reachable from s, we need to mark all of them unreachable. If the graph is connected each edge is scanned exactly twice, thus O(V+E)

6 Formal Analysis of BFS Operation ENQUEUE DEQUEUE # of times operation is executed by DFS O(max(V,E)) Total time each operation takes Θ (1) Total time taken by DFS O (E+V)  Given a graph G with vertices V and edges E.  Data Structure used : QUEUE TOTAL= O (E+V) Thanks to: Sunaina Kalucha (29 ) (MCS '11) Each vertex is enqueued at most once and dequeued the same number of times. For every vertex dequeued, its neighbours are enqueued. Hence, it follows…

7 Given a connected, undirected graph, G=(V,E), with weights on edges, MST is a spanning tree with minimum weight. Thanks To: Swati Parmar(30) MCS 2011

8 Kruskal’s MST Algorithm Kruskal’s algorithm is a greedy algorithm, because at each step it adds to the forest an edge of least possible weight. Initialize the set A to empty set and create |V| trees, one containing each vertex. The edges in E are sorted in non-decreasing order by weight For each edge (u,v), check whether the vertices u and v belong to the same tree. If yes, then discard (u,v) as it will create a cycle in the forest. Else add (u,v) to A and merge the two trees into one. Thanks To: Swati Parmar(30) MCS 2011

9 Analysis Data structure used is disjoint set structure which is implemented using linked list. Operations: Makeset(x): Creates a new set whose only member is x. It takes Θ (1) time. Findset(x): Returns a pointer to the representative of the set containing x. It takes Θ (1) time. Union(x,y): unites the dynamic set that contain x and y, say S x and S y, into a new set that is union of these two sets. It takes O(lgV) time. Thanks To: Swati Parmar(30) MCS 2011

10 OperationNo of times each operation is performed for algorithm under consideration Time each operation takes Total Time Makeset(x)VΘ(1)O(V) Findset(x)≤2E or O(E)Θ(1)O(E) Union(x,y)V-1 (or E)O(lgV)O(VlgV) Total Time =O(V+E+VlgV)=O(E + VlgV) Thanks To: Swati Parmar(30) MCS 2011 Makeset(x) is executed for each vertex. Findset(x) is executed for each edge. Union(x,y) is executed at most (V-1) times. For connected graphs V-1 ≤ E so V=O(E)

11 Thanks to Vikrant Ghai(31)

12 Prim’s MST Algorithm The tree T starts from an arbitrary vertex r and grows until the tree spans all the vertices in V. A light edge is added to the tree T that connects T to a vertex of G – T. Thus at each step, a vertex is added to the tree. Thanks to Vikrant Ghai(31)

13 The operations performed are: Makeheap() :creates and returns a new heap. Enqueue():inserts an element in the queue. Dequeue():delete an element from the queue. Decrease-key(H,x,k) :assigns to node x within heap H the new value k,which is assumed to be no greater than its current key value. Extract-min(H) :deletes the node from heap H whose key is minimum, returning a pointer to the node. Thanks to Vikrant Ghai(31)

14 Data structure: Priority Queue OperationNo of times the operation is called for algorithm under consideration Time taken by the operation Total Time EnqueueVΘ(1)O(V) Decrease-KeyE O( log v ) O(Elog v) Extract-MinV-1O(lg V)O(V lg V) Total time= O((E+V)lgV) = O(E log v) for a connected graph Thanks to Vikrant Ghai (31)


Download ppt "Instructor Neelima Gupta Table of Contents Graph Algorithms Thanks to: Sunaina Kalucha (29) (MCS '11)"

Similar presentations


Ads by Google