Graphs CS 302 – Data Structures Section 9.3. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges between the.

Slides:



Advertisements
Similar presentations
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advertisements

chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Chapter 20: Graphs CS Data Structures Mehmet H Gunes Modified from authors’ slides.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Graphs CS3240, L. grewe.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Shortest Path Problems
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Algorithms: Part 1
Graphs CS 308 – Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Shortest Path Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
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.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe.
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.
Introduction to Algorithms Jiafen Liu Sept
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
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.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
Lecture 13 Algorithm Analysis
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.
– Graphs 1 Graph Categories Strong Components Example of Digraph
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
CSE Graph Search Algorithms. CSE Graph a c b Node ~ city or computer Edge ~ road or data cable Undirected or Directed A surprisingly large.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Chapter 9 Graphs Modified from Dr George Bebis. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges between.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Chapter 9 Priority Queues, Heaps, and Graphs
UNIT – III PART - II Graphs By B VENKATESWARLU, CSE Dept.
Graphs CS Data Structures Mehmet H Gunes
Chapter 9 Graphs and Sets
Introduction to Graphs
UCS 406 – Data Structures & Algorithms
Minimum Spanning Trees
Graphs.
Shortest Path Problems
Chapter 10 The Graph ADT.
Topological Sorting Minimum Spanning Trees Shortest Path
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 .
Presentation transcript:

Graphs CS 302 – Data Structures Section 9.3

What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges between the vertices. The set of edges describes relationships among the vertices

Applications Computer networks Circuits Schedules Hypertext Maps

Formal definition of graphs A graph G is defined as follows: G=(V,E) V: a finite, nonempty set of vertices E: a set of edges (pairs of vertices)

Undirected graphs When the edges in a graph have no direction, the graph is called undirected The order of vertices in E is not important for undirected graphs!! undirected graph

When the edges in a graph have a direction, the graph is called directed. Directed graphs E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) The order of vertices in E is important for directed graphs!!

Trees are special cases of graphs!! Trees vs graphs I

Graph terminology Adjacent nodes: two nodes are adjacent if they are connected by an edge 7 is adjacent from 5 or 5 is adjacent to is adjacent from/to 5 or 5 is adjacent from/to 7

Graph terminology Path: a sequence of vertices that connect two nodes in a graph. The length of a path is the number of edges in the path. e.g., a path from 1 to 4:

Graph terminology Complete graph: a graph in which every vertex is directly connected to every other vertex

What is the number of edges E in a complete directed graph with V vertices? E=V * (V-1) Graph terminology (cont.) or E=O(V 2 )

What is the number of edges E in a complete undirected graph with V vertices? E=V* (V-1) / 2 Graph terminology (cont.) or E=O(V 2 )

Weighted graph: a graph in which each edge carries a value Graph terminology (cont.)

Graph Implementation Array-based Linked-list-based

Array-based implementation Use a 1D array to represent the vertices Use a 2D array (i.e., adjacency matrix) to represent the edges

Array-based implementation (cont’d)

Array-Based Implementation (cont.) Memory required –O(V+V 2 )=O(V 2 ) Preferred when –The graph is dense: E = O(V 2 ) Advantage –Can quickly determine if there is an edge between two vertices Disadvantage –No quick way to determine the vertices adjacent from another vertex x ?

Linked-list-based implementation Use a 1D array to represent the vertices Use a list for each vertex v which contains the vertices which are adjacent from v (adjacency list)

Linked-list-based implementation (cont’d)

Link-List-based Implementation (cont.) Memory required –O(V + E) Preferred when –for sparse graphs: E = O(V) Disadvantage –No quick way to determine whether there is an edge between vertices u and v Advantage –Can quickly determine the vertices adjacent from a given vertex O(V) for sparse graphs since E=O(V) O(V 2 ) for dense graphs since E=O(V 2 ) x ?

Graph specification based on adjacency matrix representation const int NULL_EDGE = 0; template class GraphType { public: GraphType(int); ~GraphType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void AddVertex(VertexType); void AddEdge(VertexType, VertexType, int); int WeightIs(VertexType, VertexType); void GetToVertices(VertexType, QueType &); void ClearMarks(); void MarkVertex(VertexType); bool IsMarked(VertexType) const; private: int numVertices; int maxVertices; VertexType* vertices; int **edges; bool* marks; };

template GraphType ::GraphType(int maxV) { numVertices = 0; maxVertices = maxV; vertices = new VertexType[maxV]; edges = new int[maxV]; for(int i = 0; i < maxV; i++) edges[i] = new int[maxV]; marks = new bool[maxV]; }

template GraphType ::~GraphType() { delete [] vertices; for(int i = 0; i < maxVertices; i++) delete [] edges[i]; delete [] edges; delete [] marks; }

void GraphType ::AddVertex(VertexType vertex) { vertices[numVertices] = vertex; for(int index = 0; index < numVertices; index++) { edges[numVertices][index] = NULL_EDGE; edges[index][numVertices] = NULL_EDGE; } numVertices++; }

template void GraphType ::AddEdge(VertexType fromVertex, VertexType toVertex, int weight) { int row; int column; row = IndexIs(vertices, fromVertex); col = IndexIs(vertices, toVertex); edges[row][col] = weight; }

template int GraphType ::WeightIs(VertexType fromVertex, VertexType toVertex) { int row; int column; row = IndexIs(vertices, fromVertex); col = IndexIs(vertices, toVertex); return edges[row][col]; }

template void GraphType ::GetToVertices(VertexType vertex, QueTye & adjvertexQ) { int fromIndex; int toIndex; fromIndex = IndexIs(vertices, vertex); for(toIndex = 0; toIndex < numVertices; toIndex++) if(edges[fromIndex][toIndex] != NULL_EDGE) adjvertexQ.Enqueue(vertices[toIndex]); }

Graph searching Problem: find if there is a path between two vertices of the graph (e.g., Austin and Washington) Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)

Depth-First-Search (DFS) Main idea: –Travel as far as you can down a path –Back up as little as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) DFS uses a stack !

found = false stack.Push(startVertex) DO stack.Pop(vertex) IF vertex == endVertex found = true ELSE “mark” vertex Push all adjacent, not “marked”, vertices onto stack WHILE !stack.IsEmpty() AND !found IF(!found) Write "Path does not exist" Depth-First-Search (DFS) (cont.) startVertex endVertex

startVertex endVertex (initialization)

template void DepthFirstSearch(GraphType graph, VertexType startVertex, VertexType endVertex) { StackType stack; QueType vertexQ; bool found = false; VertexType vertex; VertexType item; graph.ClearMarks(); stack.Push(startVertex); do { stack.Pop(vertex); if(vertex == endVertex) found = true; (continues)

else if(!graph.IsMarked(vertex)) { graph.MarkVertex(vertex); graph.GetToVertices(vertex, vertexQ); while(!vertexQ.IsEmpty()) { vertexQ.Dequeue(item); if(!graph.IsMarked(item)) stack.Push(item); } } while(!stack.IsEmpty() && !found); if(!found) cout << "Path not found" << endl; }

Breadth-First-Searching (BFS) Main idea: –Look at all possible paths at the same depth before you go at a deeper level –Back up as far as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) BFS uses a queue !

found = false queue.Enqueue(startVertex) DO queue.Dequeue(vertex) IF vertex == endVertex found = true ELSE “mark” vertex Enqueue all adjacent, not “marked”, vertices onto queue WHILE !queue.IsEmpty() AND !found IF(!found) Write "Path does not exist" Breadth-First-Searching (BFS) (cont.) startVertex endVertex

startVertexendVertex (initialization)

.... Duplicates: should we mark a vertex when it is Enqueued or when it is Dequeued ?

template void BreadthFirtsSearch(GraphType graph, VertexType startVertex, VertexType endVertex); { QueType queue; QueType vertexQ; bool found = false; VertexType vertex; VertexType item; graph.ClearMarks(); queue.Enqueue(startVertex); do { queue.Dequeue(vertex); if(vertex == endVertex) found = true; (continues)

else if(!graph.IsMarked(vertex)) { graph.MarkVertex(vertex); graph.GetToVertices(vertex, vertexQ); while(!vertxQ.IsEmpty()) { vertexQ.Dequeue(item); if(!graph.IsMarked(item)) queue.Enqueue(item); } } while (!queue.IsEmpty() && !found); if(!found) cout << "Path not found" << endl; } “mark” when dequeue a vertex  allow duplicates!

template void BreadthFirtsSearch(GraphType graph, VertexType startVertex, VertexType endVertex); { QueType queue; QueType vertexQ; bool found = false; VertexType vertex; VertexType item; graph.ClearMarks(); queue.Enqueue(startVertex); do { queue.Dequeue(vertex); if(vertex == endVertex) found = true; (continues) O(V) O(V) times Time Analysis

else { if(!graph.IsMarked(vertex)) { graph.MarkVertex(vertex); graph.GetToVertices(vertex, vertexQ); while(!vertxQ.IsEmpty()) { vertexQ.Dequeue(item); if(!graph.IsMarked(item)) queue.Enqueue(item); } } while (!queue.IsEmpty() && !found); if(!found) cout << "Path not found" << endl; } Arrays: O(V+V 2 +E v1 +E v2 +…)=O(V 2 +E)=O(V 2 ) O(E Vi ) times O(V) – arrays O(E vi ) – linked lists

else { if(!graph.IsMarked(vertex)) { graph.MarkVertex(vertex); graph.GetToVertices(vertex, vertexQ); while(!vertxQ.IsEmpty()) { vertexQ.Dequeue(item); if(!graph.IsMarked(item)) queue.Enqueue(item); } } while (!queue.IsEmpty() && !found); if(!found) cout << "Path not found" << endl; } Linked Lists: O(V+2E v1 +2E v2 +…)=O(V+E) O(E Vi ) times O(V) - arrays O(E vi ) – linked lists O(V 2 ) O(V) dense sparse

Shortest-path problem There might be multiple paths from a source vertex to a destination vertex Shortest path: the path whose total weight (i.e., sum of edge weights) is minimum Austin  Houston  Atlanta  Washington: 1560 miles Austin  Dallas  Denver  Atlanta  Washington: 2980 miles

Variants of Shortest Path Single-pair shortest path –Find a shortest path from u to v for given vertices u and v Single-source shortest paths –G = (V, E)  find a shortest path from a given source vertex s to each vertex v  V

Variants of Shortest Paths (cont’d) Single-destination shortest paths –Find a shortest path to a given destination vertex t from each vertex v –Reversing the direction of each edge  single-source All-pairs shortest paths –Find a shortest path from u to v for every pair of vertices u and v

Notation Weight of path p =  v 0, v 1,..., v k  Shortest-path weight from s to v : min w(p) : s v if there exists a path from s to v ∞ otherwise s tx yz p δ(v) =

Negative Weights and Negative Cycles Negative-weight edges may form negative-weight cycles. If negative cycles are reachable from the source, the shortest path is not well defined. –i.e., keep going around the cycle, and get w(s, v) = -  for all v on the cycle s ab ef -3 y c d g

Could shortest path solutions contain cycles? Negative-weight cycles –Shortest path is not well defined Positive-weight cycles: –By removing the cycle, we can get a shorter path Zero-weight cycles –No reason to use them; can remove them to obtain a path with same weight

Shortest-path algorithms Solving the shortest path problem in a brute-force manner requires enumerating all possible paths. –There are O(V!) paths between a pair of vertices in a acyclic graph containing V nodes. We will discuss two algorithms –Dijkstra’s algorithm –Bellman-Ford’s algorithm

Shortest-path algorithms (cont’d) Dijkstra’s and Bellman-Ford’s algorithms are “greedy” algorithms! –Find a “globally” optimal solution by making “locally” optimum decisions. Dijkstra’s algorithm –Does not handle negative weights. Bellman-Ford’s algorithm –Handles negative weights but not negative cycles reachable from the source.

Shortest-path algorithms (cont’d) Both Dijkstra’s and Bellman-Ford’s algorithms are iterative: –Start with a shortest path estimate for every vertex: d[v] –Estimates are updated iteratively until convergence: d[v]  δ(v)

Shortest-path algorithms (cont’d) Two common steps: (1) Initialization (2) Relaxation (i.e., update step)

Initialization Step –Set d[s]=0 (i.e., source vertex) –Set d[v]=∞ (i.e., large value) for 0   s tx

Relaxation Step Relaxing an edge (u, v) implies testing whether we can improve the shortest path to v found so far by going through u: If d[v] > d[u] + w(u, v) we can improve the shortest path to v  d[v]=d[u]+w(u,v) 59 2 uv 57 2 uv RELAX(u, v, w) 56 2 uv 56 2 uv ss no change

Bellman-Ford Algorithm Can handle negative weights. Detects negative cycles reachable from the source. Returns FALSE if negative-weight cycles are reachable from the source s  no solution

Bellman-Ford Algorithm (cont’d) Each edge is relaxed |V–1| times by making |V-1| passes over the whole edge set. To make sure that each edge is relaxed exactly |V – 1| times, it puts the edges in an unordered list and goes over the list |V – 1| times. 0   s tx yz (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)

Example 0   s tx yz   s tx yz E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y) 6 7 Pass 1

Example 0 6  7  s tx yz (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y) 0 6  7  s tx yz  7  s tx yz  7  s tx yz Pass 1 (from previous slide) Pass 2 Pass 3 Pass 4

Detecting Negative Cycles: needs an extra iteration for each edge (u, v)  E do if d[v] > d[u] + w(u, v) then return FALSE return TRUE 0   c s b   c s b c s b Consider edge (s, b): d[b] = -1 d[s] + w(s, b) = -4 d[b] > d[s] + w(s, b)  d[b]=-4 (d[b] keeps changing!) 1 st pass 2 nd pass (s,b) (b,c) (c,s)

BELLMAN-FORD Algorithm 1. INITIALIZE-SINGLE-SOURCE(V, s) 2. for i ← 1 to |V| do for each edge (u, v)  E 4. do RELAX(u, v, w) 5. for each edge (u, v)  E 6. do if d[v] > d[u] + w(u, v) 7. then return FALSE 8. return TRUE Time: O(V+VE+E)=O(VE) O(V) O(E) O(VE)

Dijkstra’s Algorithm Cannot handle negative-weights! – w(u, v) > 0,  (u, v)  E Each edge is relaxed only once!

Dijkstra’s Algorithm (cont’d) At each iteration, it maintains two sets of vertices: d[v]=δ (v)d[v]>δ (v) V SV-S (estimates have converged to the shortest path solution) (estimates have not converged yet) Initially, S is empty

Dijkstra’s Algorithm (cont.) Vertices in V–S reside in a min-priority queue Q –Priority of u determined by d[u] –The “highest” priority vertex will be the one having the smallest d[u] value.

Dijkstra (G, w, s) 0   s tx yz   s tx yz S=<> Q= S= Q= Initialization

Example (cont.) 0 10  5  s tx yz s tx yz S= Q= S= Q=

Example (cont.) s tx yz s tx yz S= Q= S= Q=<> Note: use back-pointers to recover the shortest path solutions!

Dijkstra (G, w, s) INITIALIZE-SINGLE-SOURCE( V, s ) S ←  Q ← V[G] while Q   do u ← EXTRACT-MIN(Q) S ← S  { u } for each vertex v  Adj[u] do RELAX( u, v, w ) Update Q (DECREASE_KEY) Overall: O(V+2VlogV+(E v1 +E v2 +...)logV) =O(VlogV+ElogV)=O(ElogV) build priority heap  O(VlogV) – but O(V) is a tigther bound  O(V) times  O(logV)  O(E vi )  O(logV)  O(V) O(E vi logV)

Dijkstra vs Bellman-Ford Bellman-Ford O(VE) Dijkstra O(ElogV) V2V2 V3V3 if G is sparse: E=O(V) if G is dense: E=O(V 2 ) VlogV V 2 logV if G is sparse: E=O(V) if G is dense: E=O(V 2 )

Improving Dijkstra’s efficiency Suppose the shortest path from s to w is the following: If u is the i-th vertex in this path, it can be shown that d[u]  δ (u) at the i-th iteration: –move u from V-S to S –d[u] never changes again w sx u … …

Add a flag for efficiency! INITIALIZE-SINGLE-SOURCE( V, s ) S ←  Q ← V[G] while Q   do u ← EXTRACT-MIN(Q) S ← S  { u }; for each vertex v  Adj[u] do RELAX( u, v, w ) Update Q (DECREASE_KEY) If v not marked  mark u

Example: negative weights AB C (1) Suppose we start from A d[A]=0, d[B]=d[C]=max (2) Relax (A,B), (A,C) d[B]=1, d[C]=2 Update Q: (3) S=, mark B, (4) Relax (C,B) d[B] will not be updated! S=<> Q= S=, mark A Q= S=, mark C, Q= Final values: d[A]=0 d[B]=1 d[C]=2

Eliminating negative weights Dijkstra’s algorithm works as long as there are no negative edge weights. Given a graph that contains negative weights, we can eliminate negative weights by adding a constant weight to all of the edges. Would this work?

Eliminating negative weights SA B B SA add 3 This is not going to work well as it adds more “weight” to longer paths!

weightless BFS can be used to solve the shortest graph problem when the graph is weightless or when all the weights are equal. –Path with lowest number of edges (connections). Need to “mark” vertices before Enqueue! (i.e., do not allow duplicates) Revisiting BFS

Exercises 19,21, p. 602

Using DFS/BFS, find if there is a path from “Hawaii to Alaska“