740 likes | 750 Views
Graph Algorithms. Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals. a. b. d. e. c. a. b. d. e. c. What is a Graph?. A graph G = (V, E) consists of: V: set of vertices E: set of edges connecting the vertices in V
E N D
Graph Algorithms • Terminology • Topological sort • Shortest-path algorithms • Minimum spanning tree • Graph traversals
a b d e c a b d e c What is a Graph? • A graph G = (V, E) consists of: • V: set of vertices • E: set of edges connecting the vertices in V • An edge e = (v, w) is a pair of vertices, where v, w V. Sometimes each edge is associated with a weight or a cost. • If the pair is ordered, then the graph is directed. Directed graphs are sometimes referred to as digraphs. • Examples V = {a, b, c, d, e} E = {(a, b), (a, c), (a, d), (b, e), (c, d), (c, e), (d, e)} V = {a, b, c, d, e} E = {(a, b), (a, c), (a, d), (b, e), (c, d), (c, e), (d, e)} (pairs are ordered)
Road Network of McAllen UTPA (v1) 1.7 2.3 Expressway 281 (v13) McColl Depot (v7) 7.7 2.2 Expressway 83 (v11) N. 10th St. Depot 1 (v4) 0.8 3.5 La Plaza Mall (v2) 23rd St. Depot (v12) 3.0 3.1 0.5 1.1 Airport (v3) 3.2 5.3 S. 10th St. Depot 2 (v5) 3.4 S. 10th St. Depot 1 (v6) 3.2 2.4 3.7 Nolana Depot 2 (v9) 7.3 Reynosa Bridge (v10) Nolana Depot 1 (v8)
a b d e c a b d e c Graph Terminology • Adjacent vertices: connected by an edge • Vertex w is adjacent to v if and only if (v, w) E. • In an undirected graph with edge (v, w), and hence (w, v), w is adjacent to v and v is adjacent to w. Vertex a is adjacent to c and vertex c is adjacent to a Vertex c is adjacent to a, but vertex a is NOT adjacent to c
a b d e c a b d e c Graph Terminology • Path • A path in a graph is a sequence of vertices w1, w2, w3, …, wN such that wi+1 is adjacent to wi (i.e, (wi, wi+1) E) for 1 <= i < N. • The length of a path, w1, w2, w3, …, wN, is the number of edges on the path, which is equal to N-1. • We allow a path from a vertex to itself; if this path contains no edges, then the path length is 0. abedce is a path. cdeb is a path. bca is NOT a path. acde is a path. abec is NOT a path.
a b d e c a b d e c Graph Terminology • Loops • If the graph contains an edge (v, v) from a vertex to itself, then the path v, v is sometimes referred to as a loop. • The graphs we will consider will generally be loopless. • Simple paths • A simple path is a path such that all vertices are distinct, except that the first and last could be the same. abedc is a simple path. cdec is a simple path. abedce is NOT a simple path.
a b d e c a b d e c Graph Terminology • Cycles • A cycle in a directed graph is a path of length at least 1 such that the first vertex on the path is the same as the last one; if the path is simple, then the cycle is a simple cycle. • A cycle in a undirected graph • A path of length at least 1 such that the first vertex on the path is the same as the last one. • The edges on the path are distinct. abeda is a simple cycle. abeceda is a cycle, but is NOT a simple cycle. abedc is NOT a cycle. aba is NOT a cycle. abedceda is NOT a cycle. abedca is a cycle.
a b d e c a b d e c Graph Terminology • DAGs (Directed acyclic graphs) • A directed graph is acyclic if it has no cycles. Not a DAG A DAG
e d a b d e c a b e c a b d e c a b d Graph Terminology • Connectivity • An undirected graph is connected if there is a path from every vertex to every other vertex. • A directed graph is strongly connected if there is a path from every vertex to every other vertex.
e b a c a b d e a c d d c a b d c b Graph Terminology • Connectivity (cont) • If a directed graph is not strongly connected, but the underlying graph (without direction to the arcs) is connected, then the graph is said to be weakly connected. • A complete graph is a graph in which there is a edge between every pair of vertices. How many edges are there in a complete graph with N vertices? Undirected graph: N(N-1)/2 Directed graph: N(N-1)
Eulerian Tour Classical Graph Problem “Can I walk across each bridge exactly once and return to the starting point?” • Eulerian tour: path that traverses every edge exactly once and return to the first vertex. • Euler’s theorem: A graph has a Eulerian tour if and only if all vertices have even degree. • Do you find such ideas interesting?
1 3 6 4 2 7 5 Representation of Graphs • Assumption • Consider directed graphs (undirected graphs are similarly represented) • All vertices are numbered starting at 1. • Adjacency matrix representation • Adjacency list representation
2 1 3 6 4 2 6 7 1 3 4 7 5 5 4 5 1 1 8 6 3 1 9 9 6 2 Adjacency Matrix • A graph can be represented by using a two-dimensional array A, called an adjacency matrix. • For each edge (u, v), we set A[u][v] to 1 (true); otherwise the entry in the array is 0 (false). • If the edge has a weight associated with it, then we can set A[u][v] equal to the weight and use either a very large or a very small weight as a sentinel to indicate nonexistent edges.
1 3 6 4 2 7 5 Adjacency Matrix Discussions • The adjacency matrix representation has the merit of extreme simplicity. • However, the space requirement is (|V|2), which can be prohibitive if the graph does not have very many edges. • Dense graphs: a graph is dense if |E| = (|V|2). • Sparse graphs: if the graph is not dense, then it is sparse. • An adjacency matrix is an appropriate representation for a dense graph. • But, in most of the applications that we shall see, the graph is sparse. |E| 4|V| 3,000 intersections 3,000 vertices 12,000 edges Matrix-size: 9,000,000
1 3 6 4 2 7 5 Adjacency Lists • An adjacency list is a better solution for a sparse graph. • For each vertex, we keep a list of all adjacent vertices. • The space requirement is O(|E| + |V|), which is linear in the size of the graph. • How to find all vertices adjacent to some given vertex v?
4 2 3 4 2 1 4 5 4 7 1 2 5 7 6 5 4 3 6 7 1 3 6 4 5 7 2 4 6 3 1 Adjacency Lists for Undirected Graphs • Each edge (u, v) appears in two lists; one is associated with u, the other is associated with v. The space usage essentially doubles.
Topological Sort • What is a topological sort? A topological sort is an ordering of vertices in a directed acyclic graph G, such that if there is a path from vi to vj in G, then vj appears after vi in the ordering. • Example course prerequisite structure • A directed edge (v, w) indicated that course v must be completed before taking course w. • A topological ordering of these courses is any course sequence that does not violate the prerequisite requirement. CAP3700 MAD3305 COP4540 MAD3104 MAD3512 COP5621 MAC3311 COP3400 COP3530 CIS4610 COP3210 COP3212 CDA4101 COP4610 COP455 CDA4400 COP4225
CAP3700 MAD3305 COP4540 MAD3104 MAD3512 COP5621 MAC3311 COP3400 COP3530 CIS4610 COP3210 b d a c e v1 v2 COP3212 CDA4101 COP4610 COP455 CDA4400 COP4225 v3 v4 v5 v6 v7 More on Topological Sort • A directed graph with cycles has no topological orderings. • If the graph has a cycle, then for two vertices v and w on the cycle, v precedes w and w precedes v. • The topological ordering is not necessarily unique. v1,v2,v5,v4,v3,v7,v6 v1,v2,v5,v4,v7,v3,v6
v1 v2 v1 v5 v3 v4 v7 v6 How to Find a Topological Ordering • The basic idea • Find any vertex with no incoming edges. • Print this vertex, and remove it, along with its edges, from the graph. • Apply this same strategy to the rest of the graph, until no vertices with 0 incoming edge. • Example Output: v1 Output: v1 v2 v2 Output: v1 v2 v5 Output: v1 v2 v5 v4 v3 v4 v5 Output: v1 v2 v5 v4 v7 Output: v1 v2 v5 v4 v7 v3 v6 v7 Output: v1 v2 v5 v4 v7 v3 v6
v1 v2 v1 v5 v4 v7 v6 Cycle Detection • Using topological sorting to detect if there is a cycle in a directed graph • Perform a topological sorting on the graph. • After finishing topological sorting, if there are remaining vertices in the graph (i.e., each of those vertices has >= 1 incoming edge), then we can say the graph contains cycles. • Example Output: v1 v2 Output: v1 v2 Output: v1 v2 v5 v3 v4 v5 Output: v1 v2 v5 v4 Output: v1 v2 v5 v4 v7 v6 v7
v1 v2 v3 v4 v5 v6 v7 Topological Sorting Algorithm 1 • We define the indegree of a vertex v as the number of edges (u, v). indegree(v1) = 0 indegree(v4) = 3 indegree(v7) = 2 • Assumptions • The indegree for each vertex is stored • The graph is read into an adjacency list • The algorithm Void Graph::topsort() { Vertex v, w; for (int counter = 0; counter < NUM_VERTICES; counter ++) { v = findNewVertexOfDegreeZero(); if (v == NOT_A_VERTEX) throw CycleFound(); v.topNum = counter; //place the topological numbering of the vertex. for each w adjacent to v w.indegree --; } } 0 1 2 3 1 3 2
v1 v2 v3 v4 v5 v6 v7 Analysis of Algorithm 1 • The function findNewVertexOfIndegreeZero scans the array of vertices looking for a vertex with indegree 0 that has not already been assigned a topological number. • Since it is a simple sequential scan of the array of vertices, each call to it takes O(|V|) time. Totally there are |V| such calls, the running time of the algorithm is O(|V|2). • Can we do better? • The cause of the poor running time is the sequential scan through the array of vertices to find the vertex with indegree 0. • In each iteration, we only update the indegrees of the vertices adjacent to the vertex with indegree 0. If the graph is sparse, we would expect that only a few vertices have their indegrees updated. • However, in Algorithm 1, in the search for a vertex of indegree 0, we look at all the vertices. • Actually, we only need to pay attention to the vertices with their indegrees updated in the last iteration. 0 1 2 3 1 3 2
v1 v2 v3 v4 v5 v6 v7 A Better Implementation Algorithm 2 • The basic idea • Use a queue to keep all the (unassigned) vertices of indegree 0 • Initially, we scan the array of vertices once to place all vertices of indegree 0 on the queue. • In each iteration, remove the front from the queue and assign the topological numbering to the vertex. • When we decrement the indegrees of the adjacent vertices, we check each vertex and place it in the rear of the queue if its indegree falls to 0. • Note that the topological ordering then is the order in which the vertices dequeue. 0 1 2 3 1 3 2
Pseudocode for Algorithm 2 void Graph::topsort() { Queue q(NUM_VERTICES); int counter = 0; Vertex v, w; q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); while (!q.isEmpty()) { v = q.dequeue(); v.topNum = ++counter; // assign next number as its topological numbering for each w adjacent to v if (--w.indegree == 0) q.enqueue(w); } if (counter != NUM_VERTICES) throw CycleFound(); }
0 1 2 3 v1 1 v2 3 2 v3 v4 v5 v6 v7 Algorithm 2 Example top_ord: 1 q: 1 0 1 2 1 3 top_ord: 1 2 2 q:
Algorithm 2 Example 1 2 1 1 0 top_ord: 1 2 3 5 q: 2 1 2 1 0 3 top_ord: 1 2 5 3 4 q: 1
Algorithm 2 Example 1 2 0 4 3 top_ord: 1 2 5 4 2 3 7 q: 0 1 2 5 4 3 top_ord: 1 2 5 4 3 1 7 q: 0
Algorithm 2 Example 1 2 5 4 3 top_ord: 1 2 5 4 3 7 0 6 q: 6 1 2 5 4 3 top_ord: 1 2 5 4 3 7 6 7 q: 6
Running Time of Algorithm 2 q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); • The initialization steps take time O(|V|). • The dequeue operations are done at most once per vertex. Totally, it takes O(|V|) time. • The for loop is executed at most once per edge. It takes O(|E|) time. • The total running time is O(|V|+|E|). v = q.dequeue(); v.topNum = ++counter; // assign next number as its topological numbering for each w adjacent to v if (--w.indegree == 0) q.enqueue(w);
Shortest-Path Algorithms • Concepts • Dijkstra’s algorithm • Acyclic graphs
Weighted Graphs • Weights on the edges of a graph represent distances, costs, etc. • An example of a weighted undirected graph:
Weighted Paths • Given a weighted graph G = (V, E) with each edge (vi, vj) having a cost ci,j, the cost of a path v1v2…vm is . This is referred to as the weighted path length. • Generally, when it is not specified whether we are referring to a weighted or an unweighted path, the path is weighted if the graph is.
2 10 1 4 3 2 2 4 8 5 6 1 Shortest Paths • The problem: Given a graph G = (V, E) with non-negative edge weights, and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. v1 v2 6 v1 v6: v3 v4 v5 v6 v7 BOS LAX Currently there are no algorithms in which finding the path from s to one vertex is any faster than finding the path from s to all other vertices.
Road Network of McAllen UTPA (v1) 1.7 2.3 Expressway 281 (v13) McColl Depot (v7) 7.7 2.2 Expressway 83 (v11) N. 10th St. Depot 1 (v4) 0.8 3.5 La Plaza Mall (v2) 23rd St. Depot (v12) 3.0 3.1 0.5 1.1 Airport (v3) 3.2 5.3 S. 10th St. Depot 2 (v5) 1.2 3.4 S. 10th St. Depot 1 (v6) 3.2 2.4 3.7 Nolana Depot 2 (v9) 7.3 Reynosa Bridge (v10) Nolana Depot 1 (v8)
2 10 1 4 3 2 2 4 8 5 6 1 Dijkstra’s Algorithm • The problem: Given a graph G = (V, E) with non-negative edge weights, and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. • Each vertex is marked as either known or unknown. • A tentative distance dv is kept for each vertex to denote the shortest path length from s to v using only known vertices as intermediates. • Use pv to record the predecessor on the path from s to v. • The key idea of Dijkstra’s algorithm: • Dijkstra’s algorithm proceeds in stages. • At each stage, • Selects a vertex v, which has the smallest dv among all the unknown vertices. • Declares that the shortest path from s to v is known. • Updates the path length for the adjacent vertices • The algorithm terminates while all vertices are labeled known. v1 v2 v3 v4 v5 v6 v7
Updating the Path Length • v is the newly evaluated vertex that has the smallest dv among all the unknown vertices. • For each adjacent vertex w, if dw > dv + cv,w, then we can improve the estimate of dw: dw = dv + cv,w • It is a good idea to use v on the path to w. 60 w s 30 20 v
2 2 10 10 1 1 4 4 3 3 2 2 2 2 4 4 8 8 5 5 6 6 1 1 Illustrating Dijkstra’s Algorithm v1 v2 • Find the shortest paths from start node v1(s). v3 v4 v5 v6 v7 v1 v2 v3 v4 v5 v6 v7 v1 is selected with path length 0.
2 2 10 10 1 1 4 4 3 3 2 2 2 2 4 4 8 8 5 5 6 6 1 1 Illustrating Dijkstra’s Algorithm v1 v2 v3 v4 v5 v6 v7 Update v2, v4 v4 is selected. v1 v2 v3 v4 v5 v6 v7 Update v3, v5, v6, v7 v2 is selected.
2 2 10 10 1 1 4 4 3 3 2 2 2 2 4 4 8 8 5 5 6 6 1 1 Illustrating Dijkstra’s Algorithm v1 v2 v3 v4 v5 v6 v7 Update v5 v3 is selected. v1 v2 v3 v4 v5 v6 v7 Update v6 v5 is selected.
2 2 10 10 1 1 4 4 3 3 2 2 2 2 4 4 8 8 5 5 6 6 1 1 Illustrating Dijkstra’s Algorithm v1 v2 v3 v4 v5 v6 v7 Update v7 v7 is selected. v1 v2 v3 v4 v5 v6 v7 Update v6 v6 is selected.
2 10 1 4 3 2 2 4 8 5 6 1 Illustrating Dijkstra’s Algorithm v1 v2 v3 v4 v5 v6 v7 Update 2 v1 v2 1 2 2 v3 v4 v5 4 1 v6 v7
2 10 1 4 3 2 2 4 8 5 6 1 Dijkstra’s Algorithm Pseudocode void Graph::dijkstra(Vertex s) { Vertex v, w; /* 1*/ s.dist = 0; /* 2*/ for ( ; ; ) { /* 3*/ v = smallest unknown distance vertex; /* 4*/ if ( v == NOT_A_VERTEX) /* 5*/ break; /* 6*/ v.known = true; /* 7*/ for each w adjacent to v /* 8*/ if (!w.know) /* 9*/ if (v.dist + cvw < w.dist) { /* 10*/ decrease (w.dist to v.dist + cvw); /* 11*/ w.path = v; } } } v1 v2 v3 v4 v5 v6 v7
v1 v2 2 10 v3 v4 v5 1 4 3 2 2 v6 v7 4 8 5 6 1 Running Time • The running time depends on how the vertices are manipulated. • If we use the obvious algorithm of scanning down the array of vertices to find the minimum dv, • Each phase will take O(|V|) time to find the minimum • Thus, O(|V|2) time will be spent finding the minimum over the course of the algorithm. • The time for updating dw is constant per update, and there is at most one update per edge for a total of O(|E|). • The total running time is O(|E|+|V|2) = O(|V|2). • Good for dense graphs.
v1 v2 2 10 v3 v4 v5 1 4 3 2 2 v6 v5 v3 v7 v2 5 3 9 3 2 v6 v7 4 8 5 6 1 Running Time • Using priority queues use a priority queue to keep the dv’s for all unknown vertices. • Perform a deleteMin operation to obtain the minimum dv, which takes O(log|V|) time. • Updating dw is treated as a decreaseKey operation on a priority queue, which takes O(log|V|) time. • How to find the corresponding dv for a vertex v? • Total running time: O((|V|+|E|)log|V|). • Using the Fibonacci heap to implement Dijkstra’s algorithm, the running time is O(|E|+|V|log|V|).
Negative Edge Costs • Dijkstra’s algorithm is NOT applicable to a graph with negative edge costs. • The Bellman-Ford algorithm solves the shortest path problem in a graph with negative edge costs in O(|V||E|) time. v2 5 v1 -6 8 v3
2 10 1 4 3 2 2 4 8 5 6 1 Printing the Shortest Paths v1 v2 • Track back the start vertex s by using pv. • Example: print out the shortest path from s (v1) to v6. v6 v7 v4 v1 v1 v4 v7 v6 v3 v4 v5 v6 v7
1 6 -1 2 7 -2 5 3 4 r s v u w x 2 Shortest Paths in Directed Acyclic Graphs • What is a directed acyclic graph? A directed acyclic graph (DAG) is a directed graph with no cycles. • Shortest paths are always well defined in a DAG, even if there are negative-cost edges (no negative-cost cycles can exist). • The algorithm void Graph::shortestPathinDAG (Vertex s) { Vertex v, w; /* 1*/ s.dist = 0; for any other vertex v, v.dist = ; /* 2*/ topologically sort the vertices of G; /* 3*/ for each vertex v taken in topologically sorted order /* 4*/ do for each vertex w adjacent to v /* 5*/ do if (v.dist + cvw < w.dist) { /* 6*/ decrease (w.dist to v.dist + cvw); /* 7*/ w.path = v; } }
Illustrations Find shortest paths from s to all other vertices. 1 6 -1 2 7 -2 5 s x v u w r 3 4 2 Step 1: Initialization: 1 6 0 -1 2 7 -2 5 s x v u w r 3 4 2 Step 2: Topologically sorting: 1 6 0 -1 2 7 -2 5 s x v u w r 3 4 2
Illustrations Step 3: Performing one pass over the vertices in the topologically sorted order and updating the distances: 1 6 0 -1 2 7 -2 5 s x v u w r 3 4 2 1 6 6 0 2 -1 2 7 -2 5 s x v u w r 3 4 2 1 6 4 6 0 2 6 -1 2 7 -2 5 s x v u w r 3 4 2
Illustrations Step 3: Performing one pass over the vertices in the topologically sorted order and updating the distances: 1 6 4 6 0 2 5 -1 2 7 -2 5 s x v u w r 3 4 2 1 6 3 6 0 2 5 -1 2 7 -2 5 s x v u w r 3 4 2 1 6 3 6 0 2 5 -1 2 7 -2 5 s x v u w r 3 4 2