Download presentation
Presentation is loading. Please wait.
Published byAshley Conley Modified over 9 years ago
1
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems
2
2 9.1 Definition A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E. Each edge (arc) is a pair (v, w), where v,w V. An edge may have a weight (cost). If the pair is ordered, then the graph is directed - digraph. Vertex w is adjacent to v if and only if (v, w) E.
3
3 9.1 Definition A path is a sequence of vertices w 1, w 2, w 3,..., w N, such that (w i, w i+1 ) E for 1 i<N. The length of a path is the number of edges on the path, which is N-1. A simple path is one such that all vertices are distinct, except that the first and the last could be the same. A cycle in a directed graph is a path of length at least 1 such that w 1 = w N.
4
4 9.1 Definition A directed graph is acyclic if it has no cycles (abbreviated as DAG). The indegree of a vertex v is the number of edges (u, v). An undirected graph is connected if there is a path from every vertex to every other vertex. –A directed graph with this property is called strongly directed.
5
5 9.1 Definition –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 an edge between every pair of vertices. Examples of graph models include computer networks, job scheduling. For vertices with names, use hash table to map names to numbers.
6
6 9.1 Definition
7
7 Matrix representation –2-dimensional array, A, adjacency matrix. –For each edge (u, v), set A [u] [v] = 1; otherwise the entry is 0. –If the edge has a weight associated with it, set A [u] [v] to the weight. –Space requirement is (|V| 2 ); alright if the graph is dense, i.e., |E| = (|V| 2 ).
8
8 9.1 Definition Adjacency list representation –For each vertex, keep a list of all adjacent vertices. –For sparse graphs. –Space requirement is (|E|+|V|).
9
9 9.1 Definition
10
10 9.2 Topological Sort An ordering of vertices in a directed acyclic graph, such that if there is a path from v i to v j, then v j appears after v i in the ordering.
11
11 9.2 Topological Sort Topological ordering is not possible if there is a cycle in the graph. A simple algorithm –Compute the indegree of all vertices from the adjacency information of the graph. –Find any vertex with no incoming edges. –Print this vertex, and remove it, and its edges. –Apply this strategy to the rest of the graph. –Running time is (|V| 2 ).
12
12 9.2 Topological Sort /* Figure 9.5 Simple topological sort */ void Topsort (Graph G) { int Counter; Vertex V, W; for (Counter=0; Counter<NumVertex; Counter++) { V = FindNewVertexOfDegreeZero ();
13
13 9.2 Topological Sort if (V == NotAVertex) { Error ("Graph has a cycle"); break; } TopNum [V] = Counter; for each W adjacent to V Indegree [W] --; }
14
14 9.2 Topological Sort An improved algorithm –Keep all the unassigned vertices of indegree 0 in a queue. –While queue not empty Remove a vertex in the queue. Decrement the indegree of all adjacent vertices. If the indegree of an adjacent vertex becomes 0, enqueue the vertex. –Running time is (|E|+|V|).
15
15 9.2 Topological Sort
16
16 9.3 Shortest Path Algorithms /* Figure 9.7 Pseudocode for Topological sort */ void Topsort (Graph G) { Queue Q; int Counter = 0; Vertex V, W; Q = CreateQueue (NumVertex); MakeEmpty (Q); for each vertex V if (Indegree [V] == 0)
17
17 9.3 Shortest Path Algorithms Enqueue (V, Q); while (!IsEmpty (Q)) { V = Dequeue (Q); TopNum [V] = ++Counter; /* Next No. */ for each W adjacent to V if (--Indegree [W] == 0) Enqueue (W, Q); }
18
18 9.3 Shortest Path Algorithms if (Counter != NumVertex) Error ("Graph has a cycle"); DisposeQueue (Q); /* Free the memory */ }
19
19 9.3 Shortest Path Algorithms Single-Source Shortest Path Problem Given an input a weighted graph, G = (V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. Assume there is no edge with a negative cost (which could create negative cost cycles).
20
20 9.3 Shortest Path Algorithms Unweighted Shortest Paths for Single- Source Graphs A simple algorithm 1.Mark the starting vertex, s 2.Find all unmarked vertices adjacent to s (vertices adjacent to s) 3.Find all unmarked vertices adjacent to the vertices marked in 2. 4.Repeat Step 3. until no more vertices can be marked.
21
21 9.3 Shortest Path Algorithms The strategy is known as breadth-first search. For each vertex, it is required to keep track of –whether the vertex has been marked (known) –its distance from s (d v ) –previous vertex of the path from s (p v ) Running time is (|V| 2 ).
22
22 9.3 Shortest Path Algorithms
23
23 9.3 Shortest Path Algorithms /* Fig. 9.16 Unweighted shortest path algorithm */ void Unweighted (Table T) /* Assume T is initialized */ { int CurrDist; Vertex V, W; for (CurrDist=0; CurrDist<NumVertex; CurrDist++) for each vertex V
24
24 9.3 Shortest Path Algorithms if (!T [V].Known && T [V].Dist == CurrDist) { T [V].Known = True; for each W adjacent to V if (T [W].Dist == Infinity) { T [W].Dist = CurrDist + 1; T [W].Path = V; } }}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.