Title: Chapter 9 Graph Algorithms
1Chapter 9 Graph Algorithms
- Fundamentals of graph theory
- Algorithms for some graph problems
- Choice of data structures for graph problems
29.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.
39.1 Definition
- A path is a sequence of vertices w1, w2, w3, ...,
wN, such that (wi, wi1) ? E for 1?iltN. - 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 w1 wN.
49.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 connected.
59.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.
69.1 Definition
79.1 Definition
- 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 ?(V2) alright if the
graph is dense, i.e., E ?(V2).
89.1 Definition
- Adjacency list representation
- For each vertex, keep a list of all adjacent
vertices. - For sparse graphs.
- Space requirement is ?(EV).
99.1 Definition
109.2 Topological Sort
- An ordering of vertices in a directed acyclic
graph, such that if there is a path from vi to
vj, then vj appears after vi in the ordering.
119.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 ?(V2).
129.2 Topological Sort
- / Figure 9.5 Simple topological sort /
- void Topsort (Graph G)
-
- int Counter
- Vertex V, W
- for (Counter0 CounterltNumVertex Counter)
-
- V FindNewVertexOfInDegreeZero ()
139.2 Topological Sort
- if (V NotAVertex)
- Error ("Graph has a cycle")
- break
-
- TopNum V Counter
- for each W adjacent to V
- Indegree W --
-
149.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 ?(EV).
159.2 Topological Sort
169.2 Topological Sort
- / 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)
179.2 Topological Sort
- 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)
-
189.2 Topological Sort
-
- if (Counter ! NumVertex)
- Error ("Graph has a cycle")
- DisposeQueue (Q) / Free the memory /
199.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).
209.3 Shortest Path Algorithms
- Unweighted Shortest Paths for Single-Source
Graphs - A simple algorithm
- 1. Mark the starting vertex, s
- 2. Find and mark all unmarked vertices adjacent
to s (vertices adjacent to s) - 3. Find and mark all unmarked vertices adjacent
to the marked vertices. - 4. Repeat Step 3. until no more vertices can be
marked.
219.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 (dv)
- previous vertex of the path from s (pv)
- Running time is ?(V2).
229.3 Shortest Path Algorithms
239.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 (CurrDist0 CurrDistltNumVertex
CurrDist) - for each vertex V
249.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
-
-
259.3 Shortest Path Algorithms
- Improvement
- Use a queue to keep all vertices whose shortest
distance to s is known. - Initialize dv to ? for all vertices, except s
- Set ds to 0 and enqueue s.
- While the queue is not empty
- remove the first vertex in the queue.
- enqueue all vertices which are adjacent to the
vertex, and have a distance of infinity from s.
269.3 Shortest Path Algorithms
- known not used
- Running time is ?(EV).
/ Fig. 9.18 Unweighted shortest path algorithm
/ void Unweighted (Table T) / Assume T is
initialized / Queue Q Vertex V, W
Q CreateQueue (NumVertex)
279.3 Shortest Path Algorithms
- MakeEmpty (Q)
- / Enqueue the start vertex S /
- Enqueue (S, Q)
- while (!IsEmpty (Q))
- V Dequeue Q)
- T V.Known True / Not really needed /
289.3 Shortest Path Algorithms
- for each W adjacent to V
- if (T W.Dist Infinity)
- T W.Dist T V.Dist 1
- T W.Path V
- Enqueue (W, Q)
-
-
- DisposeQueue (Q) / Free the memory /
299.3 Shortest Path Algorithms
- Result for Fig. 9.20, with weight1 start v3
309.3 Shortest Path Algorithms
319.3 Shortest Path Algorithms
- Dijkstras Algorithm (for single-source weighted
graphs) - A greedy algorithm, solving a problem by stages
by doing what appears to be the best thing at
each stage. - Select a vertex v, which has the smallest dv
among all the unknown vertices, and declare that
the shortest path from s to v is known.
329.3 Shortest Path Algorithms
- For each adjacent vertex, w, update dw dv
cv,w if this new value for dw is an improvement.
339.3 Shortest Path Algorithms
Application of Dijkstras algorithm for Fig. 9.20
349.3 Shortest Path Algorithms
Details on Fig. 9.28
359.3 Shortest Path Algorithms
- / Fig. 9.29 Declarations for Dijkstras
Algorithm - typedef int Vertex
- struct TableEntry
- List Header / Adjacency list /
- int Known
- DistType Dist
- Vertex Path
-
- defin NotAVertex (-1)
- tyepdef struct TableEntry Table NumVertex
369.3 Shortest Path Algorithms
- / Fig. 9.30 Table initialization routine /
- void InitTable (Vertex Start, Graph G, Table T)
- int i
- ReadGraph (G, T)
- for (i0 iltNumVertex i)
- T i.Known False T i.Dist Infinity
- T i.Path NotAVertex
-
- T Start.Dist 0
379.3 Shortest Path Algorithms
- / Fig. 9.31 Routine to print actual shortest
path / - / Assume the path exists /
- void PrintPath (Vertex V, Table T)
- if (T V.Path ! NotAVertex)
- PrintPath (T V.Path, T)
- printf (" to")
-
- printf ("v", V) / v is pseudocode /
389.3 Shortest Path Algorithms
- / Fig. 9.32 Pseudocode - Dijkstras Algorithm /
- void Dijkstra (Table T)
- Vertex V, W
- for ()
- V smallest unknown distance vertex
- if (V NotAVertex)
- break
- T V.Known True
- for each W adjacent to V
399.3 Shortest Path Algorithms
- if (!T W.Known)
- if (T V.Dist Cvw lt T W.Dist)
- / update W /
- Decrease (T W.Dist to T V.Dist
- Cvw)
- T W.Path V
-
-
409.3 Shortest Path Algorithms
- Total of the running time to find the minimum dv
is ?(V2). - Running time for updating dw is constant per
update, for a total of ?(E). - Total running time is ?(EV2).
- For a sparse graph, E ?(V), the algorithm
is slow. Its best to keep the distances in a
priority queue.
419.4 Network Flow Problems
- Given
- A directed graph G (V,E) with edge capacities
cv,w and two vertices being assigned as the
source, s and sink, t - Through any edge, (v,w), at most cv,w units of
flow may pass. - Note that for any vertices other than the source
and the sink, the amount of flow coming in must
be the same as that coming out.
42What is the maximum flow from in to out?
The problem is to find the maximum amount of flow
that can pass from s to t.
Out-flow
In-flow
439.4 Network Flow Problems
449.4.1 Maximum-Flow Algorithm
Initially
Total flow 0
Flow graph
Residual graph
Original graph
459.4.1 Maximum-Flow Algorithm
Attempt 1 Try (S-gtb-gtd-gtT) which allows 2 units
of flow
Total flow 2
Flow graph
Residual graph
Original graph
469.4.1 Maximum-Flow Algorithm
Attempt 1 Then (S-gta-gtc-gtT) which allows 2 units
of flow
Total flow 4
Flow graph
Residual graph
Original graph
479.4.1 Maximum-Flow Algorithm
Attempt 1 Third (S-gta-gtd-gtT) which allows 1 unit
of flow
Finished!
Total flow 5
Flow graph
Residual graph
Original graph
489.4.1 Maximum-Flow Algorithm
Attempt 2 Try (S-gtb-gtd-gtT) which allows 3 units
of flow
Finished!
Total flow 3
Flow graph
Residual graph
Original graph
499.4.1 Maximum-Flow Algorithm
- Its hard to know which path to choose first in
order to attain the maximum flow. - Solution
- Use of augmenting paths!
509.4.1 Maximum-Flow Algorithm
Attempt 3 Try (S-gtb-gtd-gtT) which allows 3 units
of flow
S
0
2
3
1
a
b
1
3
2
3
d
c
3
2
0
T
Total flow 3
Flow graph
Residual graph
Original graph
519.4.1 Maximum-Flow Algorithm
Attempt 3 Then (S-gtb-gtd-gta-gtc-gtT) with 2 units
of flow
S
0
0
2
3
1
a
b
3
2
1
0
2
1
d
c
2
3
0
0
T
Finished!
Total flow 5
Flow graph
Residual graph
Original graph
529.5 Minimum Spanning Tree
- Given
- An undirected graph G (V,E) with edge weight
cv,w - The problem is to find the minimum tree which
spans all the vertices in V - Note a minimum spanning tree should have the
number of edges equal V-1 (Why?)
539.5 Minimum Spanning Tree
2
2
V1
V2
V1
V2
1
1
3
10
4
2
2
7
V3
V4
V5
V4
V3
V5
4
8
4
6
6
5
1
1
V6
V7
V6
V7
549.5.1 Prims Algorithm
- Identical to Dijkstras algorithm except that the
graph is undirected (bidirectional) and
Decrease (T W.Dist to T V.Dist Cvw)
Refer to Pg 39
T W.Dist min( T W.Dist, Cvw)
552
V1
V2
V1
V2
1
1
3
10
4
2
7
V3
V4
V5
V4
V3
V5
8
4
6
5
1
V6
V7
V6
V7
562
2
V1
V2
V1
V2
1
1
3
10
4
2
7
V3
V4
V5
V4
V3
V5
8
4
6
5
1
V6
V7
V6
V7
572
2
V1
V2
V1
V2
1
1
3
10
4
2
2
7
V3
V4
V5
V4
V3
V5
8
4
6
5
1
V6
V7
V6
V7
582
2
V1
V2
V1
V2
1
1
3
10
4
2
2
7
V3
V4
V5
V4
V3
V5
4
8
4
6
5
1
V6
V7
V6
V7
592
2
V1
V2
V1
V2
1
1
3
10
4
2
2
7
V3
V4
V5
V4
V3
V5
4
8
4
6
5
1
1
V6
V7
V6
V7
602
2
V1
V2
V1
V2
1
1
3
10
4
2
2
7
V3
V4
V5
V4
V3
V5
4
8
4
6
6
5
1
1
V6
V7
V6
V7
Finished!