Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Slides:



Advertisements
Similar presentations
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Advertisements

Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
Graphs CS3240, L. grewe.
CHAPTER 13 Graphs DATA ABSTRACTION AND PROBLEM SOLVING WITH C++ WALLS AND MIRRORS Third Edition Frank M. Carrano Janet J. Prichard Data Abstraction and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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.
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.
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
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Algorithms: Part 1
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
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.
© 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.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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.
Chapter 2 Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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,
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 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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 Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
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.
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
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.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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 Chapter 20.
Graphs CS Data Structures Mehmet H Gunes
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Shortest Path Problems
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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:

Chapter 20: Graphs CS Data Structures Mehmet H Gunes Modified from authors’ slides

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

Terminology Definition: – A set of points that are joined by lines Graphs also represent the relationships among data items G = { V, E } – a graph is a set of vertices and edges A subgraph consists of a subset of a graph’s vertices and a subset of its edges Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Formally a graph G is defined as follows: G = (V,E) where – V(G) is a finite, nonempty set of vertices – E(G) is a set of edges written as pairs of vertices

An undirected graph The order of vertices in E is not important for undirected graphs!! A graph in which the edges have no direction

A directed graph A graph in which each edge is directed from one vertex to another (or the same) vertex The order of vertices in E is important for directed graphs!!

A directed graph Trees are special cases of graphs!

Terminology Undirected graphs: edges do not indicate a direction Directed graph, or digraph: each edge has a direction Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Terminology (a) A campus map as a graph; (b) a subgraph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Terminology Graphs that are (a) connected; (b) disconnected; and (c) complete Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Terminology (a) A multigraph is not a simple graph; (b) a self edge is not allowed in a simple graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Path: A sequence of vertices that connects 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 Graph terminology

Terminology Simple path: passes through vertex only once Cycle: a path that begins and ends at same vertex Simple cycle: cycle that does not pass through other vertices more than once Connected graph: each pair of distinct vertices has a path between them Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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

Terminology Complete graph: each pair of distinct vertices has an edge between them Graph cannot have duplicate edges between vertices – Multigraph: does allow multiple edges When labels represent numeric values, graph is called a weighted graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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

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

A weighted graph Weighted graph: A graph in which each edge carries a value 18

Terminology (a) a weighted graph; (b) a directed graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graphs as ADTs ADT graph operations – Test whether graph is empty. – Get number of vertices in a graph. – Get number of edges in a graph. – See whether edge exists between two given vertices. – Insert vertex in graph whose vertices have distinct values that differ from new vertex’s value. – Insert edge between two given vertices in graph. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graphs as ADTs ADT graph operations, ctd. – Remove specified vertex from graph and any edges between the vertex and other vertices. – Remove edge between two vertices in graph. – Retrieve from graph vertex that contains given value. View interface for undirected, connected graphs, Listing 20-1Listing 20-1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Array-Based Implementation Use a 1D array to represent the vertices Use a 2D array (i.e., adjacency matrix) to represent the edges Adjacency Matrix: – for a graph with N nodes, an N by N table that shows the existence (and weights) of all edges in the graph

to node x ? from node x ? Adjacency Matrix for Flight Connections

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 – Consumes significant memory for sparse large graphs

Linked 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 (i.e., adjacency list) Adjacency List: – A linked list that identifies all the vertices to which a particular vertex is connected; each vertex has its own adjacency list

Adjacency List Representation of Graphs to node x ? from node x ?

Link-List-based Implementation (cont.) Memory required – O(V + E) Preferred when – for sparse graphs: E = O(V) Disadvantage – No quick way to determine the vertices adjacent to a given vertex 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 )

Implementing Graphs (a) A directed graph and (b) its adjacency matrix Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Implementing Graphs (a) A weighted undirected graph and (b) its adjacency matrix Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Implementing Graphs (a) A directed graph and (b) its adjacency list Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Implementing Graphs (a) A weighted undirected graph and (b) its adjacency list Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graph Traversals Visits all of the vertices that it can reach – Happens if graph is connected Connected component is subset of vertices visited during traversal that begins at given vertex Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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 startVertex endVertex

Depth First Search: Follow Down DFS uses Stack ! 2 1 3

startVertex endVertex (initialization) 36

37

endVertex 38

Depth-First Search Goes as far as possible from a vertex before backing up Recursive algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search Iterative algorithm, using a stack Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search Iterative algorithm, using a stack, ctd. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search Visitation order for (a) a depth-first search; (b) a breadth-first search Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search A connected graph with cycles Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search The results of a depth-first traversal, beginning at vertex a, of the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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 startVertex endVertex

Breadth First: Follow Across BFS uses Queue !

Breadth First Uses Queue

startVertexendVertex (initialization) 48

49

50

Breadth-First Search Visits all vertices adjacent to vertex before going forward Breadth-first search uses a queue Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Breadth-First Search The results of a breadth-fi rst traversal, beginning at vertex a, of the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs A directed graph without cycles Topological Sorting Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs The graph arranged according to the topological orders (a) a, g, d, b, e, c, f and (b) a, b, g, d, e, f, c Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs Topological sorting algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs A trace of topSort1 for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs A trace of topSort1 for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs A trace of topSort1 for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs A trace of topSort1 for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs A trace of topSort2 for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees Tree: an undirected connected graph without cycles Observations about undirected graphs 1.Connected undirected graph with n vertices must have at least n – 1 edges. 2.Connected undirected graph with n vertices and exactly n – 1 edges cannot contain a cycle 3.A connected undirected graph with n vertices and more than n – 1 edges must contain at least one cycle Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees The DFS spanning tree rooted at vertex a for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees DFS spanning tree algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees BFS spanning tree algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees The BFS spanning tree rooted at vertex a for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees A weighted, connected, undirected graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees Minimum spanning tree algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees A trace of primsAlgorithm for the graph, beginning at vertex a Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees A trace of primsAlgorithm for the graph, beginning at vertex a Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees A trace of primsAlgorithm for the graph, beginning at vertex a Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graph Algorithms Depth-first search – Visit all the nodes in a branch to its deepest point before moving up Breadth-first search – Visit all the nodes on one level before going to the next level Single-source shortest-path – Determines the shortest path from a designated starting node to every other node in the graph 71

Single Source Shortest Path 72

Single Source Shortest Path What does “shortest” mean? What data structure should you use? 73

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 74

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 75

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 76

Shortest Paths Shortest path between two vertices in a weighted graph has smallest edge-weight sum (a) A weighted directed graph and (b) its adjacency matrix Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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 Notation 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 c d g 79

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 80

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 an acyclic graph containing V nodes. We will discuss two algorithms – Dijkstra’s algorithm – Bellman-Ford’s algorithm 81

Shortest-path algorithms 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. 82

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) 83

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

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

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) Relaxation Step 59 2 uv 57 2 uv RELAX(u, v, w) 56 2 uv 56 2 uv ss no change 86

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 87

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) 88

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 89

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 90

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) 91

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

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

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 94

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. 95

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

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

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

Dijkstra (G, w, s) INITIALIZE-SINGLE-SOURCE( V, s ) S ←  Q ← V[G] while Q   u ← EXTRACT-MIN(Q) S ← S  { u } for each vertex v  Adj[u] 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(V logV)  O(V) times  O(logV)  O(E vi )  O(logV)  O(V) O(E vi logV) 99

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 … … 100

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

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 ) 102

Shortest Paths Dijkstra’s shortest-path algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shortest Paths Dijkstra’s shortest-path algorithm, ctd. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shortest Paths A trace of the shortest-path algorithm applied to the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shortest Paths Checking weight [u] by examining the graph: (a) weight [2] in step 2; (b) weight [1] in step 3; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shortest Paths Checking weight [u] by examining the graph: (c) weight [3] in step 3; (d) weight [3] in step 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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

Circuits Another name for a type of cycle common in statement of certain problems Circuits either visit every vertex once or visit every edge once An Euler circuit begins at a vertex v, passes through every edge exactly once, and terminates at v Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits (a) Euler’s bridge problem and (b) its multigraph representation Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits Pencil and paper drawings Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits Connected undirected graphs based on the drawings Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits The steps to determine an Euler circuit for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits The steps to determine an Euler circuit for the graph Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems Hamilton circuit – Path that begins at a vertex v, passes through every vertex in the graph exactly once, and terminates at v The traveling salesperson problem – Variation of Hamilton circuit – Involves a weighted graph that represents a road map – Circuit traveled must be the least expensive Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems The three utilities problem Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems Planar graph – Can draw it in a plane in at least one way so that no two edges cross The four-color problem – Given a planar graph, can you color the vertices so that no adjacent vertices have the same color, if you use at most four colors? Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems 1.Describe the graphs in Figure For example, are they directed? Connected? Complete? Weighted? 2.Use the depth-first strategy and the breadth- first strategy to traverse the graph in Figure a, beginning with vertex 0. List the vertices in the order in which each traversal visits them. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems 3.Write the adjacency matrix for the graph in Figure a. 4.Add an edge to the directed graph in Figure that runs from vertex d to vertex b. Write all possible topological orders for the vertices in this new graph. 5.Is it possible for a connected undirected graph with fi ve vertices and four edges to contain a simple cycle? Explain. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems 6.Draw the DFS spanning tree whose root is vertex 0 for the graph in Figure Draw the minimum spanning tree whose root is vertex 0 for the graph in Figure What are the shortest paths from vertex 0 to each vertex of the graph in Figure a? (Note the weights of these paths in Figure ) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems FIGURE Graphs for Checkpoint Questions 1, 2, and 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems FIGURE A graph for Checkpoint Questions 6 and 7 and for Exercises 1 and 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013