Beruflich Dokumente
Kultur Dokumente
Chapter 9
Vertices
Adjacent Vertices
Edge
3
Directed Graph (also called Digraph)
5
Definitions
A cycle is a path of length >= 1 starting and
ending at the same vertex.
In a directed graph, a path of u-v-u would be a
cycle, since the (u,v) edge is different from the
(v,u) edge.
In an undirected graph, the edges of a cycle must
be distinct, so u-v-u is not a cycle.
A graph without cycles is called acyclic.
A directed acyclic graph is abbreviated DAG.
6
Path (shown in blue)
Loop
8
Cycle in a directed graph
9
Cycle in an undirected graph
10
Directed Acyclic Graph (DAG)
11
Definitions
A graph is connected if there is a path from
every vertex to every other vertex.
A directed graph with paths between all
vertices is said to be strongly connected.
A directed graph is weakly connected if its
undirected representation is connected.
A complete graph has edges between every
pair of vertices.
12
A Connected Graph
13
Graph that is not connected
14
A Weakly Connected Graph
18
An Airport System
HOU
ATL
DFW
AUS
BNA
SHV
19
An Airport System
HOU
ATL
1 2
1
DFW
.75
AUS 1 BNA
SHV
21
A Traffic Flow System
1.3 1.8
Campbell Campbell Campbell
/Coit /Floyd /75
1.1
Coit/
Arapaho
23
Adjacency Matrix
ATL 0 0 1 0 0 1
AUS 0 0 1 0 0 0
DFW 1 1 0 1 1 0
HOU 0 0 1 0 0 0
SHV 0 0 1 0 0 0
BNA 1 0 0 0 0 0
25
ATL DFW BNA
AUS DFW
DFW ATL AUS HOU SHV
HOU DFW
SHV DFW
BNA ATL
Adjacency list
26
Topological Sort
A topological sort is 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.
It is not a unique ordering.
Informally, a topological sort is a list of the
vertices of a DAG in which all the
successors of any given vertex appear in the
sequence after that vertex.
27
Topological Sort
An example would be courses and their
prerequisites.
Any ordering that does not violate the
prerequisite requirement would be a
topological sort.
28
Topological Sort
Example d
b g
a e i
c h
Other possibilites:
f S = {a,b,c,f,e,d,h,g,i}
S = {a,b,d,e,g,c,f,h,i}
S = a,b,c,d,e,f,g,h,i S = {a,c,f,b,d,e,h,g,i}
29
Algorithm to Produce a Topological Sort
35
Shortest-Path Algorithms
Single-source shortest path problem: find
the shortest weighted path from a vertex, s,
to every other vertex in the graph.
36
2
v1 v2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
38
Unweighted Shortest Paths
An unweighted shortest path problem can
be solved by treating all edges as having
weight = 1.
Therefore, it can be solved as a special case
of the weighted shortest path problem.
39
Unweighted Shortest Paths
We can determine distances from a vertex by
starting with the vertex and finding all
adjacent vertices. These have length 1.
Then we move to each of the adjacent
vertices and find all of their adjacent vertices
not already visited. These have length 2.
We continue until all vertices are visited.
This is called a breadth-first search.
40
1 2
v1 v2
2
v3 v4 v5
0 3
v6 v7
1 3
v1 v2
2
v3 v4 v5
0 3
v6 v7
1 3
v1 v2
2
v3 v4 v5
0 3
v6 v7
1 3
v1 v2
2
v3 v4 v5
0 3
v6 v7
1 3
45
Computing unweighted shortest path from v3 to all other vertices.
V known dv pv
---------------------------------------
v1 F 0
v2 F 0
v3 F 0 0
v4 F 0
v5 F 0
v6 F 0
v7 F 0
V known dv pv
---------------------------------------
v1 F 1 v3
v2 F 0
v1 v2
v3 T 0 0
v4 F 0 v3 v4 v5
v5 F 0 v6 v7
v6 F 1 v3
v7 F 0
v3 is known.
Find v1 and v6 at distance 1 (from v3).
47
Computing unweighted shortest path from v3 to all other vertices.
V known dv pv
---------------------------------------
v1 T 1 v3
v2 F 2 v1
v1 v2
v3 T 0 0
v4 F 2 v1 v3 v4 v5
v5 F 0 v6 v7
v6 T 1 v3
v7 F 0
v1 is known.
Find v2 and v4 at distance 2.
pv is v1 because that is how we reached v2 and v4.
v6 is known, has no successors.
48
Computing unweighted shortest path from v3 to all other vertices.
V known dv pv
---------------------------------------
v1 T 1 v3
v2 T 2 v1
v1 v2
v3 T 0 0
v4 T 2 v1 v3 v4 v5
v5 F 3 v2 v6 v7
v6 T 1 v3
v7 F 3 v4
v2 is known.
Find v5 at distance 3.
pv is v2 for v5 because that is how we reached v5.
v4 is known.
Find v7 at distance 3.
49
pv is v4 for v7 because that is how we reached v7.
Computing unweighted shortest path from v3 to all other vertices.
V known dv pv
---------------------------------------
v1 T 1 v3
v2 T 2 v1
v1 v2
v3 T 0 0
v4 T 2 v1 v3 v4 v5
v5 T 3 v2 v6 v7
v6 T 1 v3
v7 T 3 v4
while ( !q.isEmpty())
{
Vertex v = q.dequeue();
55
Dijkstras Algorithm
The algorithm proceeds in stages.
At each stage, it selects a vertex which has
the smallest distance among all the
unknown vertices.
It then declares this vertex known, updates
the table for the adjacent vertices if the cost
is less than the current cost, and repeats this
again for the next least distance vertex, until
all vertices are known.
56
v1 v2
2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv Find shortest path from v1
--------------------------------------- to all other vertices.
v1 F 0 0
v2 F 0 Start with v1 at distance 0.
v3 F 0
v4 F 0
v5 F 0
v6 F 0
57
v7 F 0
v1 v2
2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
--------------------------------------- Mark v1 as known.
v1 T 0 0 Update dv for adjacent
v2 F 2 v1 vertices v2 and v4.
v3 F 0 Choose v4 since it has the
v4 F 1 v1 least dv among all
v5 F 0 unknown vertices.
v6 F 0
58
v7 F 0
v1 v2
2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
--------------------------------------- Mark v4 as known.
v1 T 0 0 Update dv for adjacent
v2 F 2 v1 vertices v3, v5, v6, v7.
v3 F 3 v4 Choose v2 next since
v4 T 1 v1 it has least dv among
v5 F 3 v4 all unknown vertices.
v6 F 9 v4
59
v7 F 5 v4
v1 v2
2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
--------------------------------------- Mark v2 as known.
v1 T 0 0 Adjacent vertices:
v2 T 2 v1 v4 is already known.
v3 F 3 v4 v5 has cost 12 from v2
v4 T 1 v1 which is not better than
v5 F 3 v4 what we already have.
v6 F 9 v4 Choose v3 or v5 next,
v7 F 5 v4 we will choose v3. 60
v1 v2
2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
--------------------------------------- Mark v3 as known.
v1 T 0 0 Adjacent vertices:
v2 T 2 v1 v1 already known.
v3 T 3 v4 v6 updated to 8.
v4 T 1 v1 Choose v5 next.
v5 F 3 v4
v6 F 8 v3
61
v7 F 5 v4
v1 v2
2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
--------------------------------------- Mark v5 as known.
v1 T 0 0 Adjacent vertices:
v2 T 2 v1 v7 not improved.
v3 T 3 v4 Choose v7 next.
v4 T 1 v1
v5 T 3 v4
v6 F 8 v3
62
v7 F 5 v4
v1 v2
2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
--------------------------------------- Mark v7 as known.
v1 T 0 0 Adjacent vertices:
v2 T 2 v1 v6 improved to 6.
v3 T 3 v4 Choose v6 next.
v4 T 1 v1
v5 T 3 v4
v6 F 6 v7
63
v7 T 5 v4
v1 v2
2
10
4 1 3
2 2
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
--------------------------------------- Mark v6 as known.
v1 T 0 0 Adjacent vertices:
v2 T 2 v1 None.
v3 T 3 v4 Done.
v4 T 1 v1
v5 T 3 v4 Distances in dv.
v6 T 6 v7 Path given by pv.
64
v7 T 5 v4
void dijkstra( Vertex s )
{
for each Vertex v
{ v.dist = INFINITY; v.known = false; }
s.dist = 0; // start source vertex at distance zero
for (; ;) {
Vertex v = smallest unknown distance vertex
if ( v == null)
break;
v.known = true;
for each Vertex w adjacent to v
if (!w.known)
if (v.dist + cvw < w.dist) // cvw is cost from v to w
{ // update w
decrease (w.dist to v.dist + cvw);
w.path = v;
}
}
} 65
}
void dijkstra( Vertex s )
{ O(|V|)
for each Vertex v
{ v.dist = INFINITY; v.known = false; }
s.dist = 0; // start source vertex at distance zero
for (; ;) { O(|V|)
Vertex v = smallest unknown distance vertex
if ( v == null) O(|V|) or
break; O(log|V|)
v.known = true; using heap
for each Vertex w adjacent to v
if (!w.known) O(|E|) in total
if (v.dist + cvw < w.dist) // cvw is cost from v to w
{ // update w
decrease (w.dist to v.dist + cvw);
w.path = v;
} O(log|V|) using heap
}
} 66
}
Dijkstra
If a linear scan of the vertices is used to find the
vertex of least cost, this step takes O(|V|).
Since the loop processes each vertex for O(|V|)
time, then O(|V|2) time is required.
The inner update loop takes O(|E|) since the
update is constant time.
So, the running time is O(|E| + |V| 2).
If the graph is dense so that |E| = (|V|2), then the
algorithm is linear in the number of edges.
67
Dijkstra
If the graph is sparse, with |E| = (|V|), then the algorithm
is too slow.
It can be improved by using a priority queue for getting the
next cheapest cost vertex.
Removing each vertex is O(log|V|).
The edge updates require a O(log|V|) decreaseKey for
each edge over the course of the algorithm.
So, the running time is:
find next min edge updates
O(|V| log |V| + |E| log|V|)= O(|E| log|V|)
68
Negative Edges
Dijkstras algorithm will not work if there
are negative edges.
When a vertex is declared known, we have
taken an edge that was the cheapest,
assuming any other path would be more
costly. But this might not be so if some
edge had a negative cost.
69
v1 v2
5
4 -3
v3
70
Acyclic Graphs
If a graph is acyclic, Dijkstras algorithm
can be improved by choosing vertices in
topological order.
When a vertex is selected, its distance
cannot be lowered since there are no
incoming edges from unknown vertices.
71
Network Flow Problems
Edges of a graph can represent capacity.
In such a graph we might want to know the
maximum flow between two vertices, which
we will refer to as the source and sink.
Such problems occur in any kind of network
involving flow, such as water through pipes
or cars through streets.
72
Cuts
Consider the flow from source s to sink t.
6 units may leave s, and 6 units may enter t.
But all flow must pass through the cut,
which restricts the flow to 5.
4 s 2
Max flow = min cut. 1
a b
2 2
4
c d
3 3
t 73
Maximum Flow Algorithm
The algorithm proceeds in stages.
We first construct a flow graph, Gf.
Initially all edges show no flow.
We then construct another graph called the
residual graph, Gr, which is the difference
between the original graph and the flow graph.
Gr tells how much more flow an edge can handle.
74
Algorithm
At each stage, we find a path in Gr from s to t
called the augmenting path.
The minimum edge on this path is the amount of
flow that may be added to every edge on the path.
We then adjust Gf by this flow and reduce Gr.
When all paths have been processed, we are done.
75
Gf Gr
4 s 2 0 s 0 4 s 2
1 0 1
a b a b a b
2 2 0 0 2 2
4 0 4
c d c d c d
3 3 0 0 3 3
t t t
Start with the flow graph, Gf all 0, and the residual graph, Gr = G.
76
Gf Gr
0 s 0 4 s 2
0 1
a b a b
0 0 2 2
0 4
c d c d
0 0 3 3
t t
Choose path s,b,d,t giving a flow of 2 through this path.
Gf s Gr s
0 2 4 0
0 1
a b a b
0 2 2 0
0 4
c d c d
0 2 3 1
t t
Add this flow to Gf, remove from Gr. 77
Gf Gr
0 s 2 4 s 0
0 1
a b a b
0 2 2 0
0 4
c d c d
0 2 3 1
t t
Choose path s,a,c,t giving a flow of 2 through this path.
Gf s Gr s
2 2 2 0
0 1
a b a b
2 2 0 0
0 4
c d c d
2 2 1 1
t t
Add this flow to Gf, remove from Gr. 78
Gf Gr
2 s 2 2 s 0
0 1
a b a b
2 2 0 0
0 4
c d c d
2 2 1 1
t t
Choose path s,a,d,t giving a flow of 1 through this path.
Gf s Gr s
3 2 1 0
0 1
a b a b
2 2 0 0
1 3
Flow of 5 reaches t, c d c d
2 from one edge, 3 from
the other. 2 3 1 0
t t
Add this flow to Gf, remove from Gr. 79
Done since no more paths from s to t. Max flow s to t is 5.
Algorithm
Unfortunately, there is a problem with this
algorithm.
It is possible that the first path chosen
results in having no other paths from s to t.
This is seen on the following slides.
80
Gf Gr
4 s 2 0 s 0 4 s 2
1 0 1
a b a b a b
2 2 0 0 2 2
4 0 4
c d c d c d
3 3 0 0 3 3
t t t
Choose path s,a,d,t.
Gf 3 s 0 Gr 1 s 2
0 1
a b a b
0 0 2 2
3 1
c d c d
0 3 3 0
t t
81
Gf Gr
4 s 2 3 s 0 1 s 2
1 0 1
a b a b a b
2 2 0 0 2 2
4 3 1
c d c d c d
3 3 0 3 3 0
t t t
Choose path s,a,c,t.
Gf 4 s 0 Gr 0 s 2
0 1
a b a b
1 0 1 2
3 1
c d c d
1 3 2 0
t t
Gf Gr
3 s 2 3 s 2
0
1
0 1
a b a b
2 2 2 1 0
0 2
1 3
c d c d
1
2 3 2 0
t t 3
85
Done, flow is 5.
Minimum Spanning Tree
Find the set of graph edges (tree) that
connect all of the vertices at lowest cost.
This tree will be acyclic. Adding an edge
not in this tree creates a cycle.
Such a tree would be useful if, for example,
we wanted to wire a house with a minimum
amount of wire.
86
v1 v2
2
10
4 1 3
2 7
v3 v4 v5
8 4
5 6
v6 v7
1
Minimum
v1 v2
spanning tree 2
1
2
v3 v4 v5
4
6
v6 v7
1 87
Prims Algorithm
Prims algorithm grows the tree in successive
stages.
We first choose any vertex.
Then we choose the edge with least cost from this
vertex and add it to the tree.
We then choose the least edge between vertices in
the tree and those not in the tree and add it.
This is repeated until all vertices are in the tree.
88
2 2
4 1 1 3 2 10 1 1 2
3 2 4 7 5 3 2 4 5
8 4
5 6
6 7 6 7
1
2
1 2 1 1 2
3 4 5 3 2 4 5
4
6 7 6 7
2
1 1 2 1 1 2
3 2 4 5
3 4 5 4
6 7 6 7
1
2 2
1 1 2 1 1 2
3 2 4 5
3 4 5 4
6
6 7 6 7 89
1
Prims Algorithm
Prims algorithm, a greedy algorithm, is
essentially the same as Dijkstras algorithm
for shortest paths.
The main difference is the update rule.
90
v1 v2
2
10
4 1 3
2 7
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
---------------------------------------
v1 F 0 0
v2 F 0
v3 F 0
v4 F 0 Choose v1
v5 F 0
v6 F 0
91
v7 F 0
v1 v2
2
10
4 1 3
2 7
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
---------------------------------------
v1 T 0 0 Mark v1 as known.
v2 F 2 v1 Update adjacent vertices.
v3 F 4 v1 Choose v4.
v4 F 1 v1
v5 F 0
v6 F 0
92
v7 F 0
v1 v2
2
10
4 1 3
2 7
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
---------------------------------------
v1 T 0 0 Mark v4 as known.
v2 F 2 v1 Update adjacent vertices.
v3 F 2 v4 Choose v2 and then v3.
v4 T 1 v1
v5 F 7 v4
v6 F 8 v4
93
v7 F 4 v4
v1 v2
2
10
4 1 3
2 7
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
---------------------------------------
v1 T 0 0 Mark v2 and v3 as known.
v2 T 2 v1 Update adjacent vertices.
v3 T 2 v4 Choose v7.
v4 T 1 v1
v5 F 7 v4
v6 F 5 v3
94
v7 F 4 v4
v1 v2
2
10
4 1 3
2 7
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
---------------------------------------
v1 T 0 0 Mark v7 as known.
v2 T 2 v1 Update adjacent vertices.
v3 T 2 v4 Choose v6, and then v5.
v4 T 1 v1
v5 F 6 v7
v6 F 1 v7
95
v7 T 4 v4
v1 v2
2
10
4 1 3
2 7
v3 v4 v5
8 4
5 6
v6 v7
1
V known dv pv
---------------------------------------
v1 T 0 0 Mark v6 and v5 as known.
v2 T 2 v1 Update adjacent vertices.
v3 T 2 v4
v4 T 1 v1
v5 T 6 v7
v6 T 1 v7
96
v7 T 4 v4
v1 v2
2
1
2
v3 v4 v5
4
6
v6 v7
1
V known dv pv
---------------------------------------
v1 T 0 0 Done since all are known.
v2 T 2 v1
v3 T 2 v4 Table gives tree:
v4 T 1 v1 (v2,v1), (v3,v4), (v4,v1),
v5 T 6 v7 (v5,v7), (v6,v7), (v7, v4)
v6 T 1 v7
97
v7 T 4 v4
Prims Algorithm
Due to the similarity to Dijkstras
algorithm, the runtime analysis is the same.
The algorithm runs in O(|E| log|V|) using a
binary heap.
98
Kruskals Algorithm
A second greedy algorithm, it continually
selects the edges in order of smallest
weight, and accepts the edge if it does not
cause a cycle.
99
2 2
4 1 1 3 2 10 1 1 2
3 2 4 7 5 3 4 5
8 4
5 6
6 7 6 1
7
1
2
1 2 1 1 2
3 4 5 3 2 4 5
6 7 6 1
7
2
1 1 2 1 1 2
3 2 4 5
3 4 5 4
6 7 6 7
1
2
1 1 2 1 1 2
3 2 4 5
3 4 5 4
6
6 7 6 7 100
1 1
Kruskals Algorithm
Edge Weight Action
---------------------------------------
2
(v1,v4) 1 Accepted 4 1 1 3 2 10
(v6,v7) 1 Accepted 3 2 4 7 5
8 4
(v1,v2) 2 Accepted 5 6
(v3,v4) 2 Accepted 6 7
1
(v2,v4) 3 Rejected
(v1,v3) 4 Rejected
(v4,v7) 4 Accepted
(v3,v6) 5 Rejected
(v5,v7) 6 Accepted
101
Pseudocode for Kruskals Algorithm
public void kruskal()
{
int edgesAccepted = 0;
DisjSet ds = new DisjSet(NUM_VERTICES);
PriorityQueue<Edge> pq = new PriorityQueue<Edge>( getEdges() );
Edge e; Vertex u, v;
102
Pseudocode for Kruskals Algorithm
public void kruskal()
{
int edgesAccepted = 0;
DisjSet ds = new DisjSet(NUM_VERTICES);
PriorityQueue<Edge> pq = new PriorityQueue<Edge>( getEdges() );
Edge e; Vertex u, v;
104
// pseudocode for depth-first search
B D E
106
Depth-First Search DFS(A)
DFS(B)
A
B D E
107
Depth-First Search DFS(A)
DFS(B)
DFS(C)
A
B D E
108
Depth-First Search DFS(A)
DFS(B)
DFS(C)
A
DFS(D)
B D E
109
Depth-First Search DFS(A)
DFS(B)
DFS(C)
A
DFS(D)
DFS(E)
B D E
110
Depth-first Spanning Tree
A depth-first search of a graph can produce
a depth-first spanning tree.
An edge to an unvisited node is called a tree
edge.
An edge to a visited node is called a back
edge.
111
Back edge
A A
Tree edge
B D E B
C
C
dfs(A)
dfs(B)
dfs(C) D E
dfs(D)
dfs(E) Tree edges are to unvisited nodes.
Back edges are to visited nodes.
B D E
113
Depth-First Search DFS(A)
DFS(B)
A
B D E
114
Depth-First Search DFS(A)
DFS(B)
DFS(C)
A
B D E
115
Depth-First Search DFS(A)
DFS(B)
DFS(C)
A
DFS(D)
B D E
116
Depth-First Search DFS(A)
DFS(B)
DFS(C)
A
DFS(D)
B D E
117
Depth-First Search DFS(A)
DFS(B)
DFS(C)
A
DFS(D)
DFS(E)
B D E
118
Depth-First Search DFS(A)
DFS(B)
DFS(C)
A
DFS(D)
DFS(E)
B D E
119
Biconnectivity
A connected undirected graph is
biconnected if there are no vertices whose
removal disconnects the rest of the graph.
A B A
B D E C D F
G E
C
Biconnected Not biconnected 120
Biconnectivity
If a graph is not biconnected, the vertices
whose removal would disconnect the graph are
called articulation points.
These are important, for example, in a network
of computers, if the articulation node goes
down, the network is disconnected.
If a graph is biconnected, then a node going
down (a computer in a network or an
intersection of streets) still permits traffic to
flow.
121
Articulation Points
A depth-first search provides a way to find
all articulation points in a connected graph.
We can number the vertices (in preorder) as
we perform the depth first search. Num(v).
We also compute the lowest numbered
vertex reachable from each node by taking
zero or more edges followed by possibly
one back edge. Low(v).
122
Articulation Points
A root is an articulation point if it has more
than one child.
Any other vertex is an articulation point if
and only if v has some child w such that
Low(w) >= Num(v).
This means it cant reach anything above
this vertex without going through this
vertex.
123
B A
A 1/1
C D F
B 2/1
G E
C 3/1
125
Euler Circuits
Euler Path:
Find a path that visits each edge exactly
once.
Euler Circuit:
Find a cycle that begins and ends at the
same point that visits each edge exactly
once.
126
Euler Circuits
Euler circuits can be solved by constructing
a graph where there is a vertex at each
intersection in the figure and an edge for
each line.
127
5 6
7
8 9
4 10
3 2
1
128
Euler Circuits
Euler circuits can be found using a series of depth-
first searches.
If the search returns to a node with no untraversed
edges, the algorithm picks the first vertex on the
path that has an untraversed edge and performs a
depth-first search again from that vertex, splicing
its list of traversed vertices into the first traversal.
This continues until all edges are visited.
The resulting set of vertices is an Euler circuit.
129
C A
D B
DFS(A)
C A
D B
D B
C A
D B
D B
Splice C
in prior path
Spliced result: A, C, E, D, B, C, D, A with
new path
132
Euler Circuit
1
2 C A
E 6
7 5
3 D B
4
Spliced result: A, C, E, D, B, C, D, A
133
End of Slides
134