Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides
Terminology In the context of this course, graphs represent relations among data items G = { V, E} A graph is a set of vertices (nodes) and A set of edges that connect the vertices An ordinary line graph © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Terminology A graph and one of its subgraphs © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Terminology Examples of graphs that are either connected, disconnected, or complete © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Terminology Graph-like structures that are not graphs © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Terminology Examples of graphs © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Terminology Examples of graphs © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Terminology Graphs for Checkpoint Questions 1, 3, 4, 5 © 2017 Pearson Education, Hoboken, NJ. All rights reserved
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. 1 2 3 4
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 A graph in which the edges have no direction The order of vertices in E is not important for undirected graphs!!
A directed graph The order of vertices in E is important for 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!
Graphs as ADTs ADT graph operations Test if empty Get number of vertices, edges in a graph See if edge exists between two given vertices Add vertex to graph whose vertices have distinct, different values from new vertex Add/remove edge between two given vertices Remove vertex, edges to other vertices Retrieve vertex that contains given value © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Graphs as ADTs A C++ interface for undirected, connected graphs © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Graphs as ADTs A C++ interface for undirected, connected graphs © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Graphs as ADTs A C++ interface for undirected, connected graphs © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Implementing Graphs (a) A directed graph and (b) its adjacency matrix © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Implementing Graphs (a) A weighted undirected graph and (b) its adjacency matrix © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Implementing Graphs (a) A directed graph and (b) its adjacency list © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Implementing Graphs (a) A weighted undirected graph and (b) its adjacency list © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Implementing Graphs Adjacency list Adjacency matrix Often requires less space than adjacency matrix Supports finding vertices adjacent to given vertex Adjacency matrix Supports process of seeing if there exists an edge from vertex i to vertex j © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Adjacency Matrix for Flight Connections from node x ? to node x ?
Array-Based Implementation (cont.) Memory required O(V+V2)=O(V2) Preferred when The graph is dense: E = O(V2) Advantage Can quickly determine if there is an edge between two vertices Disadvantage Consumes significant memory for sparse large graphs
Adjacency List Representation of Graphs from node x ? to 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(V2) for dense graphs since E=O(V2)
Graph Traversals
Graph Traversals Visits all vertices it can reach Visits all vertices if and only if the graph is connected Connected component Subset of vertices visited during a traversal that begins at a given vertex © 2017 Pearson Education, Hoboken, NJ. All rights reserved
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 traversal Recursive transversal algorithm Goes as far as possible from a vertex before backing up Recursive transversal algorithm © 2017 Pearson Education, Hoboken, NJ. All rights reserved
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 3 2 1 DFS uses Stack !
endVertex startVertex (initialization)
endVertex
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
Breadth-First Search BFS traversal Visits all vertices adjacent to a vertex before going forward BFS is a first visited, first explored strategy Contrast DFS as last visited, first explored © 2017 Pearson Education, Hoboken, NJ. All rights reserved
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 1 4 3 8 7 6 2 5 BFS uses Queue !
Breadth First Uses Queue
startVertex endVertex (initialization)
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
Depth-First Search vs Breadth-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 vs Breadth-First Search A connected graph with cycles © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Depth-First Search The results of a depth-first traversal, beginning at vertex a, of the graph © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Breadth-First Search The results of a breadth-first traversal, beginning at vertex a, of the graph © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Applications of Graphs Topological Sorting Spanning Trees Minimum Spanning Trees Shortest Paths Circuits Some Difficult Problems © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Spanning Trees A tree is an undirected connected graph without cycles Detecting a cycle in an undirected graph Connected undirected graph with n vertices must have at least n – 1 edges If it has exactly n – 1 edges, it cannot contain a cycle With more than n – 1 edges, must contain at least one cycle © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Spanning Trees A spanning tree for the graph © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Spanning Trees Connected graphs that each have four vertices and three edges © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Spanning Trees Connected graphs that each have four vertices and three edges © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Spanning Trees The BFS spanning tree rooted at vertex a for the graph © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Minimum Spanning Trees A weighted, connected, undirected graph © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Minimum Spanning Trees A trace of primsAlgorithm for the graph beginning at vertex a © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Minimum Spanning Trees A trace of primsAlgorithm for the graph beginning at vertex a © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Minimum Spanning Trees A trace of primsAlgorithm for the graph beginning at vertex a © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Remember
also
and
finally
Shortest Paths
Graph Algorithms Depth-first search Breadth-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
Single Source Shortest Path
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 AustinHoustonAtlantaWashington: 1560 miles AustinDallasDenverAtlantaWashington: 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
Shortest Paths The shortest path between two vertices in a weighted graph Has the smallest edge-weight sum (a) A weighted directed graph and (b) its adjacency matrix © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Shortest Paths A trace of the shortest-path algorithm applied to the graph © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Shortest Paths Checking weight[u] by examining the graph: (a) weight[2] in step 2; (b) weight[1] in step 3; © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Shortest Paths Checking weight[u] by examining the graph: (c) weight[3] in step 3; (d) weight[3] in step 4 © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Notation min w(p) : s v if there exists a path from s to v Weight of path p = v0, v1, . . . , vk Shortest-path weight from s to v: min w(p) : s v if there exists a path from s to v ∞ otherwise 3 9 5 11 6 7 s t x y z 2 1 4 p δ(v) =
Negative Weights and Negative Cycles 3 -4 2 8 -6 s a b e f -3 5 6 4 7 c d g 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
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 an acyclic graph containing V nodes. We will discuss two algorithms Dijkstra’s algorithm Bellman-Ford’s algorithm
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.
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 Set d[v]=∞ for i.e., source vertex i.e., large value t x 5 6 -2 -3 8 7 s -4 7 2 9
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) s s 5 9 2 u v 5 6 2 u v RELAX(u, v, w) RELAX(u, v, w) 5 7 2 u v 5 6 2 u v 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 (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y) t x 5 6 -2 -3 8 7 s -4 7 2 9 y z
Example s t x y z s t x y z 6 Pass 1 7 6 5 7 9 s t x y z 8 -3 2 -4 -2 6 5 7 9 s t x y z 8 -3 2 -4 -2 6 Pass 1 7 E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
Example (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y) 6 7 5 9 s t x y z 8 -3 2 -4 -2 6 7 5 9 s t x y z 8 -3 2 -4 -2 Pass 1 (from previous slide) Pass 2 11 4 2 Pass 3 6 7 5 9 s t x y z 8 -3 2 -4 -2 11 4 Pass 4 6 7 5 9 s t x y z 8 -3 2 -4 -2 11 4 2 -2
Detecting Negative Cycles: needs an extra iteration c s b 2 3 -8 for each edge (u, v) E do if d[v] > d[u] + w(u, v) then return FALSE return TRUE 1st pass 2nd pass 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!) c s b 2 3 -8 -3 2 5 c s b 3 -8 -3 2 -6 -1 5 2 (s,b) (b,c) (c,s)
BELLMAN-FORD Algorithm INITIALIZE-SINGLE-SOURCE(V, s) for i ← 1 to |V| - 1 for each edge (u, v) E RELAX(u, v, w) if d[v] > d[u] + w(u, v) return FALSE return TRUE Time: O(V+VE+E)=O(VE) O(V) O(V) O(VE) O(E) O(E)
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: V S V-S d[v]=δ (v) d[v]≥δ (v) 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) Initialization Q=<y,t,x,z> S=<> Q=<s,t,x,z,y> S=<s> Q=<y,t,x,z> Initialization 10 1 5 2 s t x y z 3 9 7 4 6 10 1 5 2 s t x y z 3 9 7 4 6 10 5
Example (cont.) S=<s,y> Q=<z,t,x> S=<s,y,z> Q=<t,x> 10 5 1 2 s t x y z 3 9 7 4 6 8 14 5 7 10 1 2 s t x y z 3 9 4 6 8 14 13 7
Example (cont.) S=<s,y,z,t,x> Q=<> S=<s,y,z,t> Q=<x> 8 9 5 7 10 1 2 s t x y z 3 4 6 8 13 5 7 10 1 2 s t x y z 3 9 4 6 9 Note: use back-pointers to recover the shortest path solutions!
Dijkstra (G, w, s) INITIALIZE-SINGLE-SOURCE(V, s) O(V) 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+(Ev1+Ev2+...)logV) =O(VlogV+ElogV)=O(ElogV) O(V) build priority heap O(V logV) O(V) times O(logV) O(Evi) O(Evi logV) O(logV)
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 s x u …
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) mark u If v not marked
Dijkstra vs Bellman-Ford O(VE) Dijkstra O(ElogV) V2 V3 if G is sparse: E=O(V) if G is dense: E=O(V2) VlogV V2logV if G is sparse: E=O(V) if G is dense: E=O(V2)
Revisiting BFS 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
Circuits Circuit Euler Circuit Another name for type of cycle common in statement of certain types of problems Typical circuits either visit every vertex once or every edge once Euler Circuit Begins at vertex v Passes through every edge exactly once Terminates at v © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Circuits (a) Euler’s bridge problem and (b) its multigraph representation © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Some Difficult Problems Hamilton circuit Begins at vertex v Passes through every vertex exactly once Terminates at v Variation is “traveling salesperson problem” Visit every city on his route exactly once Edge (road) has associated cost (mileage) Goal is determine least expensive circuit © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Some Difficult Problems The three utilities problem © 2017 Pearson Education, Hoboken, NJ. All rights reserved