Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.

Slides:



Advertisements
Similar presentations
Introduction to Graph Theory Instructor: Dr. Chaudhary Department of Computer Science Millersville University Reading Assignment Chapter 1.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 20: Graphs CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Structures Using C++
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CS202 - Fundamental Structures of Computer Science II
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Chapter 23 Minimum Spanning Trees
Graph & BFS.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Shortest Path Problems
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Chapter 2 Graph Algorithms.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
COSC 2007 Data Structures II Chapter 14 Graphs III.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
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.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
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.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Lecture 13 Algorithm Analysis
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 Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Minimum Spanning Trees Problem Description Why Compute MST? MST in Unweighted Graphs MST in Weighted Graphs –Kruskal’s Algorithm –Prim’s Algorithm 1.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Spring 2016CS202 - Fundamental Structures of Computer Science II1 Initially prepared by Dr. Ilyas Cicekli; improved by various Bilkent CS202 instructors.
Graphs Chapter 20.
CS202 - Fundamental Structures of Computer Science II
Graphs Representation, BFS, DFS
CS202 - Fundamental Structures of Computer Science II
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graph Algorithm.
Graphs Chapter 13.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPH – Definitions A graph G = (V, E) consists of
Minimum Spanning Trees
Presentation transcript:

Graphs Part II Lecture 7

Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

Applications of Graph There are many useful applications of graphs. There are many useful applications of graphs. Some common applications are as follows Some common applications are as follows Topological Sort Spanning Tree Minimum Spanning Tree Shortest Path Traveling Salesman Problem Three Utilities Problem Graph Coloring Problem

Topological Sort A directed graph without cycles has a natural order. A directed graph without cycles has a natural order. Example : Vertex a precedes vertex b which precedes vertex c. Example : Vertex a precedes vertex b which precedes vertex c. There is a linear order, called topological order of the vertices in the directed graph without cycles In the list of vertices in topological order vertex x precedes vertex y if there is a directed edge from x to y in the graph If the graph represent academic courses then the graph represents the prerequisite structure for the courses. Course a is the prerequisite for course b which is the prerequisite for c and e. Graph

The vertex in the graph may have several topological orders. Example : a,g,d,b,e,c,f and a,b,g,d,e,f,c If you arrange the vertices of a directed graph linearly and in topological order, the edges will point in one direction Topological Sort Graph Arranging the vertices into a topological order is called topological sorting.

Simple algorithm for top sort1 Working : 1. Find a vertex which has no successor. 2. Remove it from the graph and all edges that lead to it. 3. Add it to the beginning of the list of vertices 4. Do the same for the remaining vertices in the graph. 5. Result : When the graph is empty the list of vertices will be in topological order.

1. N = number of vertices in the graph 2. For ( step = 1 through n) { select a vertex v that has no successors aList.insert (1,v) Delete from the graph vertex v and its edges } When the traversal ends the list of vertices will be in topological order. When the traversal ends the list of vertices will be in topological order. Simple algorithm for top sort1

Spanning Tree A Tree is a special kind of graph with no cycles. A Tree is a special kind of graph with no cycles. All trees are graphs but not all graphs are trees All trees are graphs but not all graphs are trees The nodes of a tree have a hierarchical arrangement that is not required of all graphs The nodes of a tree have a hierarchical arrangement that is not required of all graphs A spanning tree of a connected undirected graph G is a sub graph of G that contains all vertices of G and enough edges to form a tree. A spanning tree of a connected undirected graph G is a sub graph of G that contains all vertices of G and enough edges to form a tree.Graph Spanning Tree

There may be several spanning tree for a given graph Working : If you have a connected undirected graph with cycles and you remove edges until there are no cycles, then you will obtain a spanning tree for the graph Observation about an undirected graph to detect a cycle A connected undirected graph that has n vertices must have at least n-1 edges A connected undirected graph that has n vertices and exactly n-1 edges cannot contain a cycle A connected undirected graph that has n vertices and more than n-1 edges must contain at least one cycle

Algorithms Two algorithms are used for determining a spanning tree of graph. Two algorithms are used for determining a spanning tree of graph. These algorithms produce different spanning trees for any particular graph. These algorithms produce different spanning trees for any particular graph. 1. DFS Spanning Tree 2. BFS Spanning Tree One way to determine the spanning tree is to traverse the graph's vertices by using a Depth first search. After the traversal the graph’s vertices and the marked edges form the spanning tree called DFS spanning tree Another way to determine the spanning tree is to traverse the graph's vertices by using a Breadth first search. After the traversal the graph’s vertices and the marked edges form the spanning tree called BFS spanning tree

DFS Spanning Tree Dfstree(in v:vertex) Mark v as visited for ( each unvisited vertex u adjacent to v ) { Mark the edge from u to v Dfstree(u)} After DFS

BFS Spanning Tree Bfstree(in v:vertex) q.createQueue()q.enqueue(v) Mark v as visited while (!q.empty()) { q.dequeue(w) for ( each unvisited vertex u adjacent to w) for ( each unvisited vertex u adjacent to w) { Mark u as visited Mark edge between w and u Mark edge between w and u q.enqueue(u) q.enqueue(u)}} After BFS

Minimum Spanning Trees Objective : To minimize the length of a connecting network Objective : To minimize the length of a connecting network The resulting connection graph is connected, undirected, and acyclic, i.e., a free tree (sometimes called simply a tree). The resulting connection graph is connected, undirected, and acyclic, i.e., a free tree (sometimes called simply a tree). So a minimum spanning tree (MST) is a spanning tree of minimum weight (cost).

Example Graph Cost of the spanning tree is the sum of the cost of the edges in the spanning tree Selecting the least cost spanning tree is minimum cost spanning tree

Algorithms Two algorithms for computing MSTs: Two algorithms for computing MSTs: –Kruskal’s Algorithm –Prim’s Algorithm Both are variants of the same generic approach.Both are variants of the same generic approach.

Kruskal’s Algorithm Working : 1.If the next edge does not induce a cycle among the current set of edges, then it is added to A. 2.If it does, then this edge is passed over, and we consider the next edge in order. 3.As this algorithm runs, the edges of A will induce a forest on the vertices and the trees of this forest are merged together until we have a single tree containing all vertices. Kruskal attempts to add edges to A in increasing order of weight (lightest edge first). Image next slide

Kruskal’s Algorithm

Prim’s Algorithm Prim works by adding leaves one at a time to the current tree. Prim works by adding leaves one at a time to the current tree. Working : 1.Start with the root vertex r (it can be any vertex). 2.At each step select, a light edge connecting a vertex in tree to a vertex not in tree 3.The tree grows until it spans all the vertices

Primsalgorithm(in v:vertex) Mark vertex v as visited and include it in the minimum spanning tree while ( there are unvisited vertices) { find the least cost edge (v,u) from a visited vertex v to some unvisited vertex u Mark u as visited Add the vertex u and the edge (v,u) to the minimum spanning tree } Prim’s Algorithm Trace of this algorithm - Next Slide

Prim’s Algorithm GraphSteps Not yet complete... Leave it as exercise

Shortest Path Problem We are given a graph G = (V, E) with weight function w We are given a graph G = (V, E) with weight function w The weight of a path p = is the sum of edge weights along the path. The weight of a path p = is the sum of edge weights along the path. The shortest-path weight between two vertices, u and v, is the length of the minimum weight path from u to v The shortest-path weight between two vertices, u and v, is the length of the minimum weight path from u to v Given a directed graph G = (V, E) with edge weights and a distinguished source vertex, determine the distance from the source vertex to every vertex in the graph. Single-Source Shortest Paths

Edge weights can be negative; but in order for the problem to be well-defined there must be no cycle whose total cost is negative. Why? Edge weights can be negative; but in order for the problem to be well-defined there must be no cycle whose total cost is negative. Why? In general, we assume there are no cycles at all: In general, we assume there are no cycles at all: –Zero-weight cycles clearly can be removed as they do not affect total path weights. –Negative cycles must be removed. –Removing positive-weight cycles from a path will reduce the weight of the path, so they also should be removed.

Variants Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from every vertex. Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from every vertex. Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. If we solve the single-source shortest paths problem, we also solve this problem. Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. If we solve the single-source shortest paths problem, we also solve this problem. All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v. This can be solved by the single-source problem run for each vertex. All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v. This can be solved by the single-source problem run for each vertex.

SSP – Dijkstra’s Algorithm Working: 1. It determines the shortest path from given origin and all other vertices. 2. The algorithm uses a set of vertex set of selected vertices and an array weight where weight of the shortest path from vertex 0 to vertex v that passes through vertices in the vertex set. 3. If v is in vertex set the shortest path involves only vertices in vertex set. However if v is not in vertex set then v is the only vertex along the path that is not in vertex set. That is the path ends with an edge from a vertex in vertex set to v. 4. Initially the vertex set contains only vertex 0, and weight contains the weights of the single edge paths from vertex 0 to all other vertices. That is weight[v] equals matrix[0][v] for all v, where matrix is the adjacency matrix. Thus initially the weight is the first row of the matrix.

SSP – Dijkstra’s Algorithm Cont... After initialization find a vertex v that is not in vertex set and that minimizes weight[v]. Add v to vertex set. For all unselected vertices u are not in vertex set check the values weight[u] to ensure that they are indeed minimum. After initialization find a vertex v that is not in vertex set and that minimizes weight[v]. Add v to vertex set. For all unselected vertices u are not in vertex set check the values weight[u] to ensure that they are indeed minimum. To make this determination break the path from 0 to u into two pieces and find their weights as follows: To make this determination break the path from 0 to u into two pieces and find their weights as follows: Weight[v] = weight of the shortest path from 0 to v Matrix[v][u] = weight of the shortest edge from v to u Compare the weight[u] with weight[v] |+ matrix[v][u] and let Compare the weight[u] with weight[v] |+ matrix[v][u] and let Weight [u] = the smaller of the values weight[u] and weight[v] + matrix [v] [u] Weight [u] = the smaller of the values weight[u] and weight[v] + matrix [v] [u]

Example: Dijkstra’s Algorithm 0    e c b d a

0    e c b d a

0   7  e c b d a

0 2  7 

0 2  7 

Example: Dijkstra’s Algorithm

Pseudocode 1. n = number of vertices in the graph 2. For(v=0 through n-1) weight [v] = matrix[0][v] 3.For(step =2 through n) { find the smallest weight [v] such that v is not in vertexset Add v to vertexset //check the weight[u] for all u not in vertexset For(all vertices u not in vertexset ) if (weight [u] > weight [v] + matrix [v][u]) if (weight [u] > weight [v] + matrix [v][u]) weight [u] = weight [v] + matrix [v][u] weight [u] = weight [v] + matrix [v][u]}

Graph and the SP A trace of the shortest- path algorithm Checking weight[u] by examining the graph:

The Bellman-Ford Algorithm What happen if we have negative weight edges ? What happen if we have negative weight edges ? Not a problem if UNLESS there is a negative weight cycle. Why ? Not a problem if UNLESS there is a negative weight cycle. Why ? ab c d e What is the least distance from a to e? That is, what is  (a,e)? Take the path

The Bellman-Ford Algorithm Solves the single-source shortest-path problem when the weights may be negative. Solves the single-source shortest-path problem when the weights may be negative. It indicates whether or not there is a negative weight cycle reachable from the source vertex. It indicates whether or not there is a negative weight cycle reachable from the source vertex. –If there is, it indicates no solution exists. –If there is not, it returns the solution. It returns FALSE if the graph contains a negative weight cycle that is reachable from the source.

Bellman-Ford Algorithm Bellman-Ford(G,w,s) 1. Initialize-Single-Source(G, s) 2. for i  1 to numVertices  (u,v)  E[G] 4. Relax(u,v,w) 5.  (u,v)  E[G] 6. if d[v] > d[u] + w(u,v) 7.return FALSE 8. return TRUE Initialize-Single-Source(G,s) 1. for each vertex v  V[G] 2. d[v]   3.  [v]  NIL 4. d[s]  0 Relax(u,v,w) 1. if d[v] > d[u] + w(u,v) 2. d[v]  d[u] + w(u,v) 3.  [u]  v

Example: Bellman-Ford Algorithm 0    z x t y s

Example: Bellman-Ford Algorithm 0 7  6  z x t y s Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

Example: Bellman-Ford Algorithm z x t y s Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

Example: Bellman-Ford Algorithm z x t y s Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

Three more Applications of Graphs ……

Traveling Salesman Problem Math version: G is a complete weighted graph G is a complete weighted graph –Every vertex adjacent to every other Find minimal cycle through all vertices Find minimal cycle through all vertices –A Hamiltonian cycle with minimal weight Going through each vertex only once Going through each vertex only once Intuitive version: We have a map with all cities and distances We have a map with all cities and distances We want to visit each city once (except for origin) We want to visit each city once (except for origin) We want to find minimum distance to travel We want to find minimum distance to travel

Three utilities problem Imagine three houses A, B, C and three utilities X, Y, Z If the houses and the utilities are vertices in a graph, is it possible to connect each house to each utility with edges that do not cross one another? The answer : NO Solution for this problem is beyond our scope

Four color problem Given a planar graph, can you color the vertices so that no adjacent vertices have the same color, if you have only four colors? Answer is Yes but it is difficult to prove. Planar graph? - where none of its edges cross This problem was posed more than a century before, it was solved in 1970’s with the use of a computer