Download presentation
Presentation is loading. Please wait.
Published byHilary Allen Modified over 9 years ago
1
Graph Algorithms Terminology Topological sort Shortest-path algorithms
Minimum spanning tree Graph traversals
2
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 a b a b c c d e d e 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)
3
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 5.3 Airport (v3) 3.2 S. 10th St. Depot 2 (v5) 3.4 3.2 S. 10th St. Depot 1 (v6) 2.4 3.7 Nolana Depot 2 (v9) Reynosa Bridge (v10) 7.3 Nolana Depot 1 (v8)
4
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. a b a b c c d e d e 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
5
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. a b a b c c d e d e abedce is a path. cdeb is a path. bca is NOT a path. acde is a path. abec is NOT a path.
6
Graph Terminology Loops Simple paths
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. a b c d e a b abedc is a simple path. cdec is a simple path. abedce is NOT a simple path. c d e
7
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. a b abeda is a simple cycle. abeceda is a cycle, but is NOT a simple cycle. abedc is NOT a cycle. c d e a b aba is NOT a cycle. abedceda is NOT a cycle. abedca is a cycle. c d e
8
Graph Terminology DAGs (Directed acyclic graphs)
A directed graph is acyclic if it has no cycles. a b a b c c d e d e Not a DAG A DAG
9
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. a b a b c c d e d e a b a b c d e d e
10
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. a b a b c c d e d e a b a b d c d c How many edges are there in a complete graph with N vertices? Undirected graph: N(N-1)/2 Directed graph: N(N-1)
11
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?
12
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 1 3 6 4 2 7 5
13
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 1 3 6 4 2 7 5 8 9
14
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. 1 3 6 4 2 7 5 |E| 4|V| 3,000 intersections 3,000 vertices 12,000 edges Matrix-size: 9,000,000
15
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? 1 3 6 4 2 7 5
16
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. 3 4 2 1 3 6 4 2 7 5 1 4 5 1 4 6 1 2 5 7 6 3 2 4 7 3 4 7 6 4 5
17
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
18
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. a b c d e MAC3311 COP3210 CAP3700 MAD3104 COP3400 COP3212 COP455 MAD3305 MAD3512 COP3530 CDA4101 CDA4400 COP4540 COP5621 CIS4610 COP4610 COP4225 v1 v3 v6 v4 v2 v7 v5 v1,v2,v5,v4,v3,v7,v6 v1,v2,v5,v4,v7,v3,v6
19
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 v1 v1 v2 v2 Output: v1 v2 v5 v3 Output: v1 v2 v5 v4 v3 v4 v4 v5 v5 Output: v1 v2 v5 v4 v7 v6 v6 v7 v7 Output: v1 v2 v5 v4 v7 v3 Output: v1 v2 v5 v4 v7 v3 v6
20
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 v1 v1 v2 v2 Output: v1 v2 Output: v1 v2 v5 v3 v4 v4 v5 v5 Output: v1 v2 v5 v4 Output: v1 v2 v5 v4 v7 v6 v6 v7 v7
21
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 --; } v3 v6 v4 v2 v7 v5 v1 1 2 3 1 3 2
22
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. v3 v6 v4 v2 v7 v5 v1 1 2 3 1 3 2
23
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. v3 v6 v4 v2 v7 v5 v1 1 2 3 1 3 2
24
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();
25
Algorithm 2 Example top_ord: top_ord: 1 q: q: v3 v6 v4 v2 v7 v5 v1 1
1 2 3 top_ord: q: 1 1 1 2 1 3 top_ord: 1 2 q: 2
26
Algorithm 2 Example top_ord: 1 2 top_ord: 1 2 5 q: q: 1 2 1 1 3 5 2
top_ord: 1 2 3 q: 5 2 1 2 1 3 top_ord: 3 q: 4 1
27
Algorithm 2 Example top_ord: 1 2 5 4 top_ord: 1 2 5 4 3 q: q: 1 2 4
4 3 top_ord: 2 q: 3 7 1 2 5 4 3 top_ord: 1 q: 7
28
Algorithm 2 Example top_ord: 1 2 5 4 3 7 top_ord: 1 2 5 4 3 7 6 q:
q: 6 6 1 2 5 4 3 top_ord: 7 q: 6
29
Running Time of Algorithm 2
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|). q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); 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);
30
Shortest-Path Algorithms
Concepts Dijkstra’s algorithm Acyclic graphs
31
Weighted Graphs Weights on the edges of a graph represent distances, costs, etc. An example of a weighted undirected graph:
32
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.
33
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. 4 2 10 3 1 6 8 5 v1 v2 v1 v6: 6 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.
34
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 5.3 Airport (v3) 3.2 S. 10th St. Depot 2 (v5) 1.2 3.4 3.2 S. 10th St. Depot 1 (v6) 2.4 3.7 Nolana Depot 2 (v9) Reynosa Bridge (v10) 7.3 Nolana Depot 1 (v8)
35
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. 4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7
36
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
37
Illustrating Dijkstra’s Algorithm
Find the shortest paths from start node v1(s). 4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 v1 is selected with path length 0.
38
Illustrating Dijkstra’s Algorithm
4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 Update v2, v4 v4 is selected. 4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 Update v3, v5, v6, v7 v2 is selected.
39
Illustrating Dijkstra’s Algorithm
4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 Update v5 v3 is selected. 4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 Update v6 v5 is selected.
40
Illustrating Dijkstra’s Algorithm
4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 Update v7 v7 is selected. 4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 Update v6 v6 is selected.
41
Illustrating Dijkstra’s Algorithm
4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7 Update 2 v1 v2 1 2 v3 2 v4 v5 4 v6 1 v7
42
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; } 4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7
43
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. 4 2 10 3 1 6 8 5 v3 v6 v4 v2 v7 v5 v1
44
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|). 4 2 10 3 1 6 8 5 v3 v6 v4 v2 v7 v5 v1 2 v2 3 v3 v5 9 v6 5 v7
45
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. 5 v2 v1 -6 8 v3
46
Printing the Shortest Paths
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 4 2 10 3 1 6 8 5 v1 v2 v3 v4 v5 v6 v7
47
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; } r s v u w x 5 2 7 -1 -2 3 6 1 4
48
Illustrations Find shortest paths from s to all other vertices.
6 1 5 2 7 -1 -2 r s v u w x 3 4 2 Step 1: Initialization: 6 1 5 2 7 -1 -2 r s v u w x 3 4 2 Step 2: Topologically sorting: 6 1 5 2 7 -1 -2 r s v u w x 3 4 2
49
Illustrations Step 3: Performing one pass over the vertices in the topologically sorted order and updating the distances: 6 1 5 2 7 -1 -2 r s v u w x 3 4 2 1 6 2 6 5 2 7 -1 -2 r s v u w x 3 4 2 6 1 2 6 6 4 5 2 7 -1 -2 r s v u w x 3 4 2
50
Illustrations Step 3: Performing one pass over the vertices in the topologically sorted order and updating the distances: 6 1 2 6 4 5 5 2 7 -1 -2 r s v u w x 3 4 2 6 1 2 6 5 3 5 2 7 -1 -2 r s v u w x 3 4 2 6 1 2 6 5 3 5 2 7 -1 -2 r s v u w x 3 4 2
51
Analysis Correctness When a vertex v is selected, its distance v.dist can no longer be lowered, since by the topological ordering rule it has no incoming edges emanating from unknown vertices. Running time Initialization step takes O(|V|) time. Topologically sorting takes O(|V|+|E|) time. In step 3, one pass over the vertices takes O(|V|) time; It totally takes O(|E|) to examine each edge once for distance updating. Totally, the running time is O(|V|+|E|). r s v u w x 5 2 7 -1 -2 3 6 4 1
52
Minimum Spanning Tree Motivation Model as a graph
A town has a set of houses and a set of roads A road connects 2 and only 2 houses A road connecting houses u and v has a repair cost c(u, v) Objective: Repair enough (and no more) roads such that Everyone stays connected: can reach every house from all other houses, and Total repair cost is minimum. Model as a graph Undirected graph G = (V, E) Weight c(u, v) on each edge (u, v) E Find T E such that T connected all vertices (T is a spanning tree), and c(T) = (u, v)T c(u, v) is minimized. 2 v1 v2 1 10 4 3 v3 2 v4 7 v5 8 4 5 6 v6 v7 1
53
Minimum Spanning Tree Given an undirected graph G = (V, E), with edge costs cij. A spanning tree T of G is an acyclic subgraph that spans all the vertices. 4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1 2 v1 v2 4 1 v3 v4 v5 8 4 6 v6 1 v7 2 2 v1 v2 v1 v2 v1 v2 10 1 10 4 1 4 3 7 2 v3 v4 v5 v3 v4 v5 v3 v4 v5 4 5 4 5 v6 1 v7 v6 v7 v7
54
Minimum Spanning Tree What is the number of edges in a spanning tree with |V| vertices? The cost of the spanning tree T is the sum of the costs of the edges in T. The minimum spanning tree (MST) is the smallest cost spanning tree |V|-1 4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1 2 v1 v2 v1 v2 4 1 10 1 10 3 2 v3 v4 v5 v3 v4 v5 8 5 6 v6 1 v6 v7 v7
55
Prim’s Algorithm Key idea
Grows the minimum spanning tree in successive stages At any point in the algorithm, we have a set of vertices that have already been included in the tree; the rest of the vertices have not. The algorithm then finds, at each stage, a new vertex to add to the tree by choosing the edge (u, v) such that the cost of (u, v) is the smallest among all edges where u is in the tree and v in not. 4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1 4 2 10 3 1 7 6 8 5 v1 v2 v3 v4 v5 v6 v7
56
Simple Way to Represent a Tree
In a tree, each vertex has only one parent. Each vertex keeps a pointer pointing to its parent. We use pv to denote the parent of v in the tree. 4 2 10 1 6 8 v3 v6 v4 v2 v7 v5 v1
57
Pseudocode of Prim’s Algorithm
4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1 Input: Graph G = (V, E), and s is the root of the minimum spanning tree. For each vertex v, A(v) is a list of its adjacent vertices. Let dv denote the cost of the shortest edge connecting v to a vertex in the tree. Let Q denote the vertices not in the tree. The algorithm void Graph::MST-Prim (Vertex s) { Q = V; for each v Q do dv = ; ds = 0; ps = null; while Q do find a vertex v Q incident on a shortest edge connecting with a vertex in the tree; add v into the tree and remove v from Q; for each vertex w adjacent to v do if w Q and cvw < dw { dw = cvw; pw = v; } 4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1
58
Illustration Grow a minimum spanning tree of G with root v1.
dv denote the cost of the shortest edge connecting v to a vertex in the tree. 4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1 2 4 2 10 3 1 7 6 8 5 v1 v2 4 2 10 3 1 7 6 8 5 v1 v2 1 v3 v4 v5 4 v3 v4 v5 v6 v7 v6 v7
59
Illustration 2 2 v1 v2 v1 v2 1 1 2 v3 v4 v5 7 2 v3 v4 v5 7 v6 v7 v6 v7
2 2 4 2 10 3 1 7 6 8 5 4 2 10 3 1 7 6 8 5 v1 v2 v1 v2 1 1 2 v3 v4 v5 7 2 v3 v4 v5 7 v6 v7 v6 v7 8 4 8 4 2 2 4 2 10 3 1 7 6 8 5 4 2 10 3 1 7 6 8 5 v1 v2 v1 v2 1 1 2 v3 v4 v5 7 2 v3 v4 v5 6 v6 v7 v6 v7 5 4 1 4
60
Illustration 2 2 v1 v2 v1 v2 1 1 2 v3 v4 v5 2 v3 6 v4 v5 6 v6 v7 v6 v7
2 2 4 2 10 3 1 7 6 8 5 4 2 10 3 1 7 6 8 5 v1 v2 v1 v2 1 1 2 v3 v4 v5 2 v3 6 v4 v5 6 v6 v7 v6 v7 1 4 1 4
61
Running Time of Prim’s Algorithm
Depend on how to implement Q. The algorithm void Graph::MST-Prim (Vertex s) { Q = V; for each v Q do dv = ; ds = 0; ps = null; while Q do find a vertex v Q incident on a shortest edge connecting with a vertex in the tree; add v into the tree and remove v from Q; for each vertex w adjacent to v do if w Q and cvw < dw { dw = cvw; pw = v; } Using an array or a list: O(|V|2) Using a binary heap: O(|E|log|V|) 4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1
62
Kruskal’s Algorithm Initially make each vertex of V a singleton tree.
Scan edges of E in non-decreasing order of cost. For edge e: If both endpoints of e in the same tree, then discard it; Otherwise, add e and merge the two trees. 4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1 4 2 10 3 1 7 6 8 5 v1 v2 v3 v4 v5 v6 v7
63
Illustration Grow a minimum spanning tree of G by using Kruskal’s algorithm. 4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1 4 2 10 3 1 7 6 8 5 4 2 10 3 1 7 6 8 5 v1 v2 v1 v2 v3 v3 v4 v5 v4 v5 v6 v7 v6 v7
64
Illustration v1 v2 v1 v2 v3 v4 v5 v3 v4 v5 v6 v7 v6 v7 v1 v2 v1 v2 v3
10 3 1 7 6 8 5 4 2 10 3 1 7 6 8 5 v1 v2 v1 v2 v3 v4 v5 v3 v4 v5 v6 v7 v6 v7 4 2 10 3 1 7 6 8 5 4 2 10 3 1 7 6 8 5 v1 v2 v1 v2 v3 v4 v5 v3 v4 v5 v6 v7 v6 v7 4 2 10 3 1 7 6 8 5 v1 v2 v3 v4 v5 v6 v7
65
Pseudocode of Kruskal’s Algorithm
4 2 10 3 1 7 6 8 5 v3 v6 v4 v2 v7 v5 v1 Input: Graph G = (V, E. For each vertex v, A(v) is a list of its adjacent vertices. The algorithm void Graph::MST-Kruskal (Vertex s) { view each vertex in V as a singleton tree; sort the edges of E by nodecreasing cost c; for each edge (v, w) E, in order by nodecreasing cost do if v and w are not in the same tree then merge the two trees; } Running Time: Using disjoint set data structure, O(|E|log|V|). 4 2 10 3 1 7 6 8 5 v1 v2 v3 v4 v5 v6 v7
66
Breadth First Search The problem
Input: Graph G = (V, E), either directed or undirected, and source vertex s V. Output: d[v] = distance (smallest # of edges) from s to v, for all v V. Idea: Send a wave out from s. First hits all vertices 1 edge from s. From there, hits all vertices 2 edges from s. Etc. s c a b e f g h q 1 3 2
67
Breadth First Search Use FIFO queue Q to maintain wavefront.
v Q if and only if wave has hit v but has not come out of v yet. BFS(G, s) for each u V-{s} do d[u] = ; [u] = null d[s] = 0; [s] = null Q = ENQUEUE(Q, s) while Q do u = DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] = d[u] + 1 [v] = u ENQUEUE(Q, v) Example: s c a b e f g h q 1 3 2
68
Breadth First Search Can show that Q consists of vertices with d values. i i i … i i+1 i+1 … i+1 Only 1 or 2 different values If 2, differ by 1 and all smallest are first. Since each vertex gets a finite d value at most once, values assigned to vertices are monotonically increasing over time. Running Time: O(|V|+|E|) O(|V|) because every vertex enqueued at most once. O(|E|) because every vertex dequeued at most once and we examine (u, v) only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected.
69
Depth-First Search Pseudocode for DFS on undirected graphs
Depth-first search (DFS) on undirected graphs We start at vertex v, mark v as visited. Recursively call depth-first search on all adjacent vertices that are not already visited. Pseudocode for DFS on undirected graphs void Graph::dfs(vertex v) { v.visited = true; for each w adjacent to v if (!w.visited) dfs(w); } DFS on graphs is similar to preorder traversal on a tree.
70
Illustration of DFS on Undirected Graphs
DFS graph G starting at vertex A A B D C E A B D E C Depth-first spanning tree
71
Depth-First Spanning Tree with Back Edges
DFS graph G starting at vertex C A B D C E F G B A D If, when we process (v, w), we find that w is unmarked, we indicate this with a tree edge. C The root of the tree is C, the first vertex visited. Each edge (v, w) in the graph is present in the tree. If, when we process (v, w), we find that w is already marked, we draw a dashed line, which we call a back edge. G E F
72
Running Time of DFS DFS is called on each vertex exactly once.
For every edge is examined exactly twice, once from each of its vertices. Total running time: O(|V|+|E|)
73
DFS on Directed Graphs Using the same strategy as with undirected graphs, directed graphs can be traversed in linear time, using depth-first search. If the graph is not strongly connected, a depth-first search starting at some node might not visit all nodes. In this case we repeatedly perform depth-first searches, staring at some unmarked node, until all vertices have been visited.
74
Illustration of DFS on Directed Graphs
DFS on directed graph G starting at vertex B A B A C D F G E H I J B G F D C H E I J Depth-first spanning forest
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.