# Chapter 9 Graph Algorithms - PowerPoint PPT Presentation

PPT – Chapter 9 Graph Algorithms PowerPoint presentation | free to view - id: 9f533-ZTM0Z The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## Chapter 9 Graph Algorithms

Description:

### Algorithms for some graph problems. Choice of data structures for graph problems. 2 ... A simple algorithm ... 9.3 Shortest Path Algorithms ... – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 61
Provided by: CSD5151
Category:
Tags:
Transcript and Presenter's Notes

Title: Chapter 9 Graph Algorithms

1
Chapter 9 Graph Algorithms
• Fundamentals of graph theory
• Algorithms for some graph problems
• Choice of data structures for graph problems

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
9.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.

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 connected.

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
9.1 Definition
7
9.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).

8
9.1 Definition
• For each vertex, keep a list of all adjacent
vertices.
• For sparse graphs.
• Space requirement is ?(EV).

9
9.1 Definition
10
9.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.

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
• 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).

12
9.2 Topological Sort
• / Figure 9.5 Simple topological sort /
• void Topsort (Graph G)
• int Counter
• Vertex V, W
• for (Counter0 CounterltNumVertex Counter)
• V FindNewVertexOfInDegreeZero ()

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
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 ?(EV).

15
9.2 Topological Sort
16
9.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)

17
9.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)

18
9.2 Topological Sort
• if (Counter ! NumVertex)
• Error ("Graph has a cycle")
• DisposeQueue (Q) / Free the memory /

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
9.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.

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 (dv)
• previous vertex of the path from s (pv)
• Running time is ?(V2).

22
9.3 Shortest Path Algorithms
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 (CurrDist0 CurrDistltNumVertex
CurrDist)
• for each vertex V

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

25
9.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.

26
9.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)
27
9.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 /

28
9.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 /

29
9.3 Shortest Path Algorithms
• Result for Fig. 9.20, with weight1 start v3

30
9.3 Shortest Path Algorithms
31
9.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.

32
9.3 Shortest Path Algorithms
• For each adjacent vertex, w, update dw dv
cv,w if this new value for dw is an improvement.

33
9.3 Shortest Path Algorithms
Application of Dijkstras algorithm for Fig. 9.20
34
9.3 Shortest Path Algorithms
Details on Fig. 9.28
35
9.3 Shortest Path Algorithms
• / Fig. 9.29 Declarations for Dijkstras
Algorithm
• typedef int Vertex
• struct TableEntry
• int Known
• DistType Dist
• Vertex Path
• defin NotAVertex (-1)
• tyepdef struct TableEntry Table NumVertex

36
9.3 Shortest Path Algorithms
• / Fig. 9.30 Table initialization routine /
• void InitTable (Vertex Start, Graph G, Table T)
• int i
• for (i0 iltNumVertex i)
• T i.Known False T i.Dist Infinity
• T i.Path NotAVertex
• T Start.Dist 0

37
9.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 /

38
9.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

39
9.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

40
9.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.

41
9.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.

42
What 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
43
9.4 Network Flow Problems
44
9.4.1 Maximum-Flow Algorithm
Initially
Total flow 0
Flow graph
Residual graph
Original graph
45
9.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
46
9.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
47
9.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
48
9.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
49
9.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!

50
9.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
51
9.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
52
9.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?)

53
9.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
54
9.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)
55
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
56
2
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
57
2
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
58
2
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
59
2
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
60
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
Finished!