Graph Theory - Brock University

Download Report

Transcript Graph Theory - Brock University

Graph Theory
© Dave Bockus
1
Adjacency Matrix
1
2
4
3
6
5
7
2
Adjacency List
1
2
4
3
5
6
7
1
2
4
2
4
6
6
4
5
3
4
5
7
7
3
3
6
7
6
3
Example DAG
C
I
E
F
A
B
J
H
G
D
Possible Topological Order
I B D G C E F J A H or C E G A I B F J D H
4
TopSort
void topsort( ) throws CycleFound {
Queue q;
int counter = 0;
Vertex v,w;
q = new Queue();
for each vertex v
if (indegree == 0)
q.enqueue(v);
while (!q.empty()) {
v = q.dequeue();
v.topNum = ++counter;
for each w adjacent to v
if (--w.indegree==0)
q.enqueue(w);
}
if (counter != NUM_VERTICES)
throw new CycleFound();
}
Initialize Q with vertices
of indegree 0
Decrement indegrees of
vertices w, if 0 then
enqueue
Vertices will remain unenumerated if a cycle
exists
5
TopSort Example
Queue
I
C
G
E
B
D
A
F
J
H
C
I
Load Q with initial v
with indegree == 0
E
F
A
B
J
H
G
D
Output
I
C
G
B
E
D
A
F
J
H
6
Shortest Path
Unweighted edges
Q.Enqueue(v0);
while (!Q.IsEmpty) {
V = Q.Dequeue();
for (Each W adjacent to V)
if(T[W].Dist == Maxint) {
T[W].Dist = T[V].Dist + 1;
T[W].Path = V;
Q.Enqueue(W);
}
}
Breadth First
Search Algorithm
7
Shortest Path - Unweighted edges
Queue
3
1
1
6
2
6
5
2
4
3
4
5
7
Ignore
V4
Ignore
V6
Queue
is
Ignore
V4
V7
V6
No vertices
Enqueue
Vo now
V4
MaxInt
adjacent
6stop
V6
!=!=
MaxInt
V4empty
V7
V6
!=toso
MaxInt
7
v
v1
v2
v3 = v0
v4
v5
v6
v7
dv
pv
1
MaxInt
2
MaxInt
v3
0
2
MaxInt
v1
v1
v2
3
MaxInt
1
MaxInt
v3
3
MaxInt
v4
8
Dijkstra’s Algorithm
Q.Enqueue(v0);
while (!Q.IsEmpty) {
do {
V = Q.Dequeue();
while (T[V].Known);
Only accept unknown
edges
T[V].Known = true;
for (Each W adjacent to V)
if(T[W].Dist > T[V].Dist + C(V,W) {
T[W].Dist = T[V].Dist + C(V,W);
T[W].Path = V;
Q.Enqueue(W);
}
}
Modify the path if an
improvement to dv exists
9
Dijkstra’s Algorithm cont...
PQueue
2
3
7
6
0 1 2
3
5
6 8 9
1
4
2
1
4
2
3
1
10
2
2
4
3
5
4
8
6
5
6
7
1
No
Queue
improvement
is now
Update
V6
isdv
dv
already
and
pv
No
Update
improvement
and
pv
No
improvement
to
empty
v4
v7
so
skip
stop
known
toreflect
reflect
soso
ignore
Enqueue
Vo
tototo
v1
so
skip
v4 so skip
improvement
improvement
6
6
5
12
v known dv
v1 = v0 10
0
10
2
v2
MaxInt
10
3
v3
MaxInt
1
v4
01 MaxInt
10
12
v5
MaxInt
1
98
6
v6
0 MaxInt
10
5
v7
MaxInt
pv
v1
v4
v1
v2
v4
v3
v7
v4
10
Kruskal's Algorithm
Build priority Queue of all edges
Edges_Accepted = 0;
while (Edges_Accepted < NumVertex - 1) {
E = Dequeue();
// E=(u,v)
if (!Connected(E.u,E.v)) {
Edges_Accepted ++;
T[Action] = true;
}
}
11
Kruskal's Algorithm cont...
2
1
2
4
10
3
1
2
2
4
3
5
4
8
6
5
6
7
1
Total path lengths:
Edge
causes
aa
V-1
Edges
accepted
Edge
causes
= 1 + 1 + 2 + so
2 +stop
2+4
= 12
cycle,
cycle, so
so ignore
ignore
Edge
V1,V4
V6,V7
V1,V2
V3,V4
V4,V5
V2,V4
V1,V3
V4,V7
V3,V6
V5,V7
V4,V6
V2,V5
Weight Action
1
1
0
1
01
2
01
1
2
0
1
2
0
3
0
4
0
1
4
0
5
0
6
0
8
0
10
0
12
Kruskal's Algorithm cont...
void kruskal() {
int edgesAccepted =0;
DisjSet ds = new DisjSets(NUM-Vertices);
PQ<Edge> pq = new PriorityQueue<Edge>(getEdges());
Edge e;
Vertix u,v;
while (edgesAccepted < NUM_VERTICES -1) {
e = pq.deleteMin(); //remove the smallest edge from pq E=(u,v);
SetType uset = ds.find(u);
SetType vset = ds.find(v);
if (uset != vset){
edgesAccepted++;
ds.union(uset,vset);
}
}
}
13
Kruskal's Algorithm cont...
• Typically the algorithm will maintain a forest of sets
– The concept would be to reduce the forest to just 1 set.
– A free tree
• This implies set operations such as:
– Union(Uset,Vset)
• Join 2 sets Uset & Vset
– Find(u)
• Return the set with vertex u in it.
– Compare(Uset,Vset)
• Is Uset equal Vset
• Set operations can become costly.
14
Kruskal's Algorithm cont...
• Determining cycles without using sets
– Support an Adjacency list of accepted edges
– Want to add edge (u,v)
– Build a tree from u of currently accepted edges.
• If v in the tree, then (u,v) will cause a cycle.
• Else we add (u,v) as an accepted edge.
• Additional Data Structures are required
– Adjacency list of accepted edges
– Integer array size n used for book keeping
• Only enumerate a vertex once.
• If graph has directed edges then we need to determine:
– u contains v
– v contains u
15
Kruskal's Algorithm cont...
for (i==1; i <= n; i++)
Known[i]=0;
//Initialize Known to 0
boolean Find(u,v){
boolean cycle = false;
Known[u]=1;
//Mark u as processed
if (u == v) return true;
//We found a cycle
for (each w adjacent to u)
if (Known[w]==0)
//Only search vertices w
cycle = cycle || Find(w,v); //which are unprocessed
return cycle;
}
If we ever find a cycle (u==v) then we return true.
This value is passed back true the recursion.
16
Prim’s Algorithm
• Basic Idea
1. Build a tree starting at Vo=u.
2. Select vertex v which is closest to u and add (u,v) to
tree.
3. Find next closes vertex v to tree and add.
4. Repeat 3 Until all v have been consumed.
17
Prim’s Algorithm
Q.Enqueue(V0,V0);
Vertices=1
while (Vertices++ < |V|) {
do {
E = Q.Dequeue();
while (T[v].Known);
}
Where E = (u,v)
PQ of edges
c(u,v), where u is
in the tree and v
is not.
T[v].Known = true;
for (Each w adjacent to v)
if(T[w].Dist > C(v,w) && !T[w].known){
T[w].Dist = C(v,w);
T[w].Path = v;
Q.Enqueue(v,w);
}
Very Similar to Dijkstra’s
Algorithm. dv now only
holds the edge weight.
18
Prim’s Algorithm Cont...
PQ
C(1,1)
C(1,4)
C(4,1)
C(4,3)
C(2,1)
C(7,6)
C(6,7)
C(1,2)
C(2,1)
C(3,4)
C(3,1)
C(1,3)
C(4,3)
C(4,2)
C(7,4)
C(4,2)
C(2,4)
C(3,6)
C(1,3)
C(7,5)
C(6,3)
C(4,7)
C(4,5)
C(7,5)
2
1
4
2
10
3
1
7
2
4
3
5
4
8
6
5
6
7
1
C(3,1)
C(4,6)
C(4,5)
C(3,6)
C(2,5)
C(4,6)
C(2,5)
C(4,5)
C(6,4)
C(4,6)
C(2,5)
C(2,5)
v known dv
v1 = v0 10
0
10 MaxInt
2
v2
10 MaxInt
2
v3
10 MaxInt
1
v4
6
10 MaxInt
v5
10 MaxInt
1
v6
10 MaxInt
4
v7
pv
V1
V4
V1
V7
V7
V4
19
Prim’s Algorithm Cont..
• The Algorithm stops when we have accepted |V|
vertices.
• We can read the accepted edges from the table
directly.
20