Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Single Source Shortest Paths
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Greed is good. (Some of the time)
Instructor Neelima Gupta Table of Contents Graph Algorithms Thanks to: Sunaina Kalucha (29) (MCS '11)
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Chapter 8, Part I Graph Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Minimum Spanning Tree By Jonathan Davis.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
IS 2610: Data Structures Graph April 5, 2004.
CS112A1 Spring 2008 Practice Final. ASYMPTOTIC NOTATION: a)Show that log(n) and ln(n) are the same in terms of Big-Theta notation b)Show that log(n+1)
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Chapter 2 Graph Algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
Runtime O(VE), for +/- edges, Detects existence of neg. loops
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs Upon completion you will be able to:
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Minimum Spanning Tree Graph Theory Basics - Anil Kishore.
Graph Search Applications, Minimum Spanning Tree
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CS120 Graphs.
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Minimum Spanning Trees
CSE 417: Algorithms and Computational Complexity
Lecture 14 Minimum Spanning Tree (cont’d)
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham

Contents  Different Parts of Vertices used in Graph Algorithms  Analysis of DFS  Analysis of BFS  Minimum Spanning Tree  Kruskal’s Algorithm  Shortest Path’s  Dijkshitra’s Algorithm

Vertices of the Graph are kept in 3 parts as below: Vertices of the Graph are kept in 3 parts as below:  Visited : Vertices already selected.  Fringe : Vertices to be considered for next selection. next selection.  Unvisited: Vertices yet to consider as a possible candidate. Visited Fringe Unvisited Different Parts of Vertices (used in Graph Algorithms)  For DFS, the Fringe Part is kept in a STACK while for BFS QUEUE is used. QUEUE is used.

Brief Description of DFS Explores a graph using greedy method. Explores a graph using greedy method. Vertices are divided into 3 parts as it proceeds i.e. into Visited, Fringe & Unvisited Vertices are divided into 3 parts as it proceeds i.e. into Visited, Fringe & Unvisited Fringe part is maintained in a Stack. Fringe part is maintained in a Stack. The path is traced from last visited node. The path is traced from last visited node. Element pointed by TOS is the next vertex to be considered by DFS algorithm to select for Visited Part. Element pointed by TOS is the next vertex to be considered by DFS algorithm to select for Visited Part.

Data Structures use in DFS (U/V/F) 1-D Array Adjacency list Fringe (Stack) VFUFUVFUFU abcdeabcde TOS d a b e d c Graph with 5 Vertices b ce a bed ea d b c c d b

Analysis of DFS For a Graph G=(V, E) and n = |V| & m=|E|  When Adjacency List is used Complexity is O(m+n) Complexity is O(m+n)  When Adjacency Matrix is used This again requires a stack to maintain the fringe & an array for maintaining the state of a node. This again requires a stack to maintain the fringe & an array for maintaining the state of a node. Scanning each row for checking the connectivity of a Vertex is in order O(n). Scanning each row for checking the connectivity of a Vertex is in order O(n). So, Complexity is O(n 2 ). So, Complexity is O(n 2 ). 2

Applications of DFS To check if a graph is connected. The algorithm ends when the Stack becomes empty. To check if a graph is connected. The algorithm ends when the Stack becomes empty. Graph is CONNECTED if all the vertices are visited. Graph is CONNECTED if all the vertices are visited. Graph is DISCONNECTED if one or more vertices Graph is DISCONNECTED if one or more vertices remained unvisited. remained unvisited. Finding the connected components of a disconnected graph. Repeat 1 until all the vertices become visited. Next vertex can also be taken randomly. Finding the connected components of a disconnected graph. Repeat 1 until all the vertices become visited. Next vertex can also be taken randomly. Check if a graph is bi-connected (i.e. if 2 distinct paths exist between any 2 nodes.). Check if a graph is bi-connected (i.e. if 2 distinct paths exist between any 2 nodes.). Detecting if a given directed graph is strongly connected ( path exists between a-b & b-a). Detecting if a given directed graph is strongly connected ( path exists between a-b & b-a). Hence DFS is the champion algorithm for all connectivity problems

Analysis of BFS Fringe part is maintained in a Queue. Fringe part is maintained in a Queue. The trees developed are more branched & wider. The trees developed are more branched & wider.  When Adjacency List is used Complexity is O(m+n) Complexity is O(m+n)  When Adjacency Matrix is used This requires a Queue to maintain the fringe & an array for maintaining the state of a node. This requires a Queue to maintain the fringe & an array for maintaining the state of a node. Scanning each row for checking the connectivity of a Vertex is in order O(n). Scanning each row for checking the connectivity of a Vertex is in order O(n). So, Complexity is O(n 2 ). So, Complexity is O(n 2 ).

Minimum Spanning Tree (MST) A spanning tree of a connected Graph G is a sub-graph of G which covers all the vertices of G. A spanning tree of a connected Graph G is a sub-graph of G which covers all the vertices of G. A minimum-cost spanning tree is one whose edge weights add up to the least among all the spanning trees. A minimum-cost spanning tree is one whose edge weights add up to the least among all the spanning trees. A given graph may have more than one spanning tree. A given graph may have more than one spanning tree. DFS & BFS give rise to spanning trees, but they don’t consider weights. DFS & BFS give rise to spanning trees, but they don’t consider weights.

Properties MST  The least weight edge of the graph is always present in the MST. Proof by contradiction: Let this be a MST. Also let us assume that the shortest edge is not part of this tree. Consider node g & h. All edges on the path between these nodes are longer.(w1,w2,w3) Let us take any edge (ex-w1) & delete it. We will then connect g & h directly. But this edge has a lighter weight then already existing edges which is a contradiction. g h w1 w2 w3 CONTRADICTION c d

…. Properties of MST  For any pair of vertices a & b, the edges along the path from a to b have to be greater than the weight of (a,b).

Kruskal’s Algorithm An algorithm to find the minimum spanning tree of connected graph. An algorithm to find the minimum spanning tree of connected graph. It makes use of the previously mentioned properties. It makes use of the previously mentioned properties. Step 1: Initially all the edges of the graph are sorted based on their weights. Step2: Select the edge with minimum weight from the sorted list in step 1.Selected edge shouldn’t form a cycle. Selected edge is added into the tree or forest. Step 3: Repeat step 2 till the tree contains all nodes of the graph.

…. Kruskal’s Algorithm This algorithm works because when any edge is rejected it will be longer than the already existing edge(s). This algorithm works because when any edge is rejected it will be longer than the already existing edge(s). The Union-Find data structure is tailor made to implement this algorithm. The Union-Find data structure is tailor made to implement this algorithm. Cycle formation between any 2 vertices a & b is checked if Find(a)=Find(b). Cycle formation between any 2 vertices a & b is checked if Find(a)=Find(b). Applet Demo Link. Applet Demo Link. Applet Demo Link. Applet Demo Link.

Analysis of Kruskal’s Alg. If an adjacency list is used. If an adjacency list is used. We have n vertices & m edges the following steps are followed. Sort the edges – mlogm Sort the edges – mlogm Find operation – 2m= O(m) Find operation – 2m= O(m) Union - n-1= O(n) Union - n-1= O(n) O(mlogm)= O(mlogn )= O(mlog n). If we use path compression then find & union become m times inverse Ackerman’s function. But sorting always takes mlogm time. 2

Shortest Paths The problem is to find shortest paths among nodes. The problem is to find shortest paths among nodes. There are 3 different versions of this problem as shown below: There are 3 different versions of this problem as shown below: 1. Single source single destination shortest path (SSSP) 2. Single source all destinations shortest path (SSAP). 3. All pairs shortest path. For un-weighted graphs: For un-weighted graphs: BFS can be used for 1. BFS can be used for 1. BFS can used to for 2. BFS can used to for 2. Running BFS from all nodes is presently the best algorithm for 3. Running BFS from all nodes is presently the best algorithm for 3.

Dijkshitra’s Algorithm This is an algorithm for finding the shortest path in a weighted graph. This is an algorithm for finding the shortest path in a weighted graph. It finds the shortest path from a single source node to all other nodes. It finds the shortest path from a single source node to all other nodes. The shortest path from a single node to all destinations is a tree. The shortest path from a single node to all destinations is a tree. The following applet gives a very good demonstration of the Dijkshitra’s Algorithm. It finds the shortest distances from 1 city to all the remaining cities to which it has a path. The following applet gives a very good demonstration of the Dijkshitra’s Algorithm. It finds the shortest distances from 1 city to all the remaining cities to which it has a path. Applet

…. Dijkshitra’s Algorithm DIJKSTRA (G, w, s) DIJKSTRA (G, w, s) 1.INITIALIZE-SINGLE-SOURCE(G, s) 2.S ← ø 3.Q ← V[G] 4.while Q ≠ ø 5. do u ← EXTRACT-MIN(Q) 6. S ← S U {u} 7. for each vertex v Є Adj[u] do 8. if d[v] > d[u] + w (u, v) 9. then d[v] ← d[u] + w (u, v) 10. π [v] ← u The algorithm repeatedly selects the vertex u with the minimum shortest-path estimate, adds u to S, and relaxes all edges leaving u. The algorithm repeatedly selects the vertex u with the minimum shortest-path estimate, adds u to S, and relaxes all edges leaving u.

…. Analysis of Dijkshitra’s Alg.  For a Graph G=(V, E) and n = |V| & m=|E| When Binary Heap is used When Binary Heap is used  Complexity is O((m+n) log n) When Fibonacci Heap is used When Fibonacci Heap is used  Complexity is O( m + n log n)

Thank you. Thank you.