• Keine Ergebnisse gefunden

Basic graph algorithms

N/A
N/A
Protected

Academic year: 2022

Aktie "Basic graph algorithms"

Copied!
68
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

§  Example problems

Let G=(V,E) be a graph (directed or undirected).

– Problem 1:

§  Input: Graph G in adjacency-list form.

§  Output: „yes“, if G contains a cycle; „no“ otherwise – Problem 2:

§  Input: Graph G in adjacency-list form.

§  Output: Number of graph-components.

– Problem 3:

§  Input: Graph G in adjacency-list form.

§  A spannning tree for each component..

29.05.12 | Komplexität | 67

Basic graph algorithms

(2)

§  Depth-First-Search (dfs)

– Input: Graph G=(V,E) in form of a adjacency-list adj[v], i.e. an adjacency-list is given for each node.

Output: edge set T (spanning forrest) and edge set B = V \ T.

– Idea:

§  Discover the graph node by node, starting from the last discovered one.

§  If all adjacent nodes of the last discoved node v have been discovered before already, jump back to that node which was discovered just before v.

Distinguish spanning tree nodes and cycle nodes.

§  If nodes stay undiscovered, start a new dfs on such a node.

29.05.12 | Komplexität | 68

Basic graph algorithm

(3)

§  Depth-First-Search, dfs

– For orientation:

§  At the beginning:all nodes arecolored white.

§  Discovered nodes become grey.

§  Completely examined nodes become black.

§  There are two time stamps: d[v] and f[v] (bewteen1 and 2|V|)

§  d[v]: v is dicovered (discover-time)

§  f[v]: v is finished (finishing-time)

29.05.12 | Komplexität | 69

Basic graph algorithms

(4)

29.05.12 | Komplexität | 70

DFS(G) Depth-First-Search (dt.: Tiefensuche)

1.  for each node u∈V do

2.  color[u] := white; „:=“: variable assignment

3.  π[u] := nil; π stores predecessors (Vorgänger)

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u); „==“: test on equality

Basic graph algorithms

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(5)

29.05.12 | Komplexität | 71

DFS(G)

1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

Basic graph algorithms

(6)

29.05.12 | Komplexität | 72

DFS(G)

1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

Basic graph algorithms

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(7)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 73

Basic graph algorithms

time = 0

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(8)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 74

Basic graph algorithms

time = 0

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(9)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 75

Basic graph algorithms

time = 0 u

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(10)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 76

Basic graph algorithms

time = 0 u

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(11)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 77

Basic graph algorithms

time = 1 u 1

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(12)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 78

Basic graph algorithms

time = 1 u 1

v

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(13)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 79

Basic graph algorithms

time = 1 u 1

v

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(14)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 80

Basic graph algorithms

time = 1 1

u

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(15)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 81

Basic graph algorithms

time = 2 1

u 2

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(16)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 82

Basic graph algorithms

time = 2 1

u 2

v DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(17)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 83

Basic graph algorithms

time = 2 1

u 2

v DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(18)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 84

Basic graph algorithms

time = 2 1

2

u DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(19)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 85

Basic graph algorithms

time = 3 1

2

u 3 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(20)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 86

Basic graph algorithms

time = 3 1

2

u 3 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(21)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 87

Basic graph algorithms

time = 3 1

2

u 3 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(22)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 88

Basic graph algorithms

time = 4 1

2

u 3,4 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(23)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 89

Basic graph algorithms

time = 4 1

u 2

3,4 v

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(24)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 90

Basic graph algorithms

time = 4 1

u 2

3,4 v

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(25)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 91

Basic graph algorithms

time = 5 1

2

3,4 u

5 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(26)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 92

Basic graph algorithms

time = 5 1

2

v

3,4 u

5 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(27)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 93

Basic graph algorithms

time = 5 1

2

v

3,4 u

5 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(28)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 94

Basic graph algorithms

time = 5 1

2

3,4

u 5

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(29)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 95

Basic graph algorithms

time = 6 1

2

3,4

u 5

6 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(30)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 96

Basic graph algorithms

time = 6 1

2

3,4

u 5

6

v DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(31)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 97

Basic graph algorithms

time = 6 1

2

3,4

u 5

6

v DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(32)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 98

Basic graph algorithms

time = 6 1

2

3,4

u 5

6 v

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(33)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 99

Basic graph algorithms

time = 6 1

2

3,4

u 5

6 v

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(34)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 100

Basic graph algorithms

time = 7 1

2

3,4

u 5

6,7 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(35)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 101

Basic graph algorithms

time = 7 1

2

u 3,4 5

6,7 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(36)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 102

Basic graph algorithms

time = 8 1

2

u 3,4 5,8

6,7 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(37)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 103

Basic graph algorithms

time = 8 1

2

3,4 u

5,8

6,7 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(38)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 104

Basic graph algorithms

time = 9 1

2,9

5,8 3,4

6,7

u

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(39)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 105

Basic graph algorithms

time = 8 1

3,4 u

5,8

6,7

2,9 DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(40)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 106

Basic graph algorithms

time = 10 1,10

2,9

5,8 3,4

6,7 u

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(41)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 107

Basic graph algorithms

time = 10 1,10

2,9

5,8 3,4

6,7 u

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(42)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 108

Basic graph algorithms

time = 11 1,10

2,9

5,8 3,4

6,7 u 11

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(43)

DFS(G) 1.  for each node u∈V do

2.  color[u] := white;

3.  π[u] := nil;

4.  time := 0;

5.  for each node u∈V do

6.  if color[u]==white then DFS-Visit(u);

29.05.12 | Komplexität | 109

Basic graph algorithms

time = 12 1,10

2,9

5,8 3,4

6,7 u 11,12

DFS-Visit(u)

1.  color[u] := gray;

2.  time := time+1; d[u] := time;

3.  for each node v∈adj[u] do 4.  if color[v]==white then

5.  π[v]:=u;

6.  DFS-Visit(v);

7.  color[u] := black

8.  time:=time+1;f[u]:=time

(44)

29.05.12 | Komplexität | 110

Basic graph algorithms

1,10

2,9

5,8 3,4

6,7 u 11,12

§  Depth-First-Search, dfs

– DFS algorithmus partitions the edges into four subsets

§  tree edges are

→ edges of the spanning forest

§  back edges are

→ edges (u,v), connecting u with earlier discovered predecessors of v

§  forward edges are

→ non-tree edges (u,v), wich connect u with earlier discovered successors of v.

§  cross edges are all other edges.

– Runtime of DFS is O(|V|+|E|)

(45)

§  Paranthesis theorem

– Let u and v b nodes of a Graph G. When the DFS search has been finished, one of the following three statements is true:

§  The intervals [d[u],f[u]] and [d[v],f[v]] are disjoint

§  The interval [d[u],f[u]] is completely contained in [d[v],f[v]], and u is a successor of v in the DFS tree

§  The interval [d[v],f[v]] is completely contained in the interval [d[u],f[u]], and v is a successor of u in the DFS-tree.

29.05.12 | Komplexität | 111

Basic graph algorithms

(46)

29.05.12 | Komplexität | 112

Basic graph algorithms

time = 12 1,10

2,9

5,8 3,4

6,7

u 11,12

w v

t s

x

s t

u v

w

x

1 2 3 4 5 6 7 8 9 10 11 12

(47)

29.05.12 | Komplexität | 113

Basic graph algorithms

§  Paranthesis Theorem, Proof

– Let d[u]<d[v]. Then, there are two cases:

1.  d[v]<f[u]: v has been discovered when u was still grey. Thus, v is successor of u . Because v has been discovered later then u, it follows that all outgoing edges of v were examined before the search process returned to u.

Therefore, firstly v and then u were finished. Therfore f[v]<f[u]. In summary : d[u]<d[v]<f[v]<f[u].

2.  f[u]<d[v]: v was discovered when u had been already finished. Of course are d[u]<f[u] and d[v]<f[v] , and thus, the intervals [d[u],f[u]] and [d[v],f[v]] are disjoint.

–  The case d[v]<d[u] is shown analogously.

(48)

29.05.12 | Komplexität | 114

Basic graph algorithms

§  White-Path Theorem

– In a DFS-forest of a directed or undirected graph G=(V,E), a node v is

successor of another node u if and only if node v can be reached from u via white nodes at time d[u].

⇒: Let v be a successor of u, such that d[v]>d[u] . Moreover, let u not yet

finished. Obviously, the path between u and v was white at time d[u], because DFS has moved from u to v with the help of line 4 of DFS-Visit.

u

v

(49)

29.05.12 | Komplexität | 115

Basic graph algorithms

§  White-Path Theorem

⇐: Assume, there is a white path from u to v at time d[u]. Wlog. we assume that v does not become a successor of u and every other node than v becomes a successor of u. (otherwise we do the following thoughts for the first non-

successor of u) Let w be the last node on the white path from u to v becoming successor of u. Then obviously, it is valid: f[w]<f[u].

Because d[v]>d[u] and d[v]<f[w] (v is direct successor of w), it is valid:

d[u]<(d[w]<)d[v]<f[w]<f[u]. With the help of the paranthesis follows f[v] < f[w].

After all, one of our assumptions does not hold; v must be a successor of u.

u

w becomes

successor v

Time: d[u]

(50)

29.05.12 | Komplexität | 116

Basic graph algorithms

§  Let G=(V,E) an undirected graph. Then is valid:

– There are only tree- and backward edges in the DFS-forest.

– The set of all tree edges builds a forest. Each connecting component generates a spanning tree.

– G is cycle-free if and only if the DFS-forest contains no backward edges.

Proof: Exercise

(51)

29.05.12 | Komplexität | 117

Shortest Path Algorithms

Computation of shortest paths in graphs

•  A weighted graph G is a tupel (V,E) together with a weight-function f, where E⊆V × V und f: E →

•  Let u,v ∈ V. A shortest path from u to v is a path with smallest possible weight from

u to v- The weight of a path is the sum of the edge weights on this path.

(52)

29.05.12 | Komplexität | 118

Shortest Path Algorithms

Computation of shortest paths in graphs

•  Single Source Shortest Path Problem

•  given: weighted graph G and start node s

•  wanted: the distance δ(s,v) for each node v as well as the shortest path

•  Shortest paths between all pairs of nodes

•  Claim: Let G be a directed graph with non-negative edge weights. If a path w from s to t is a shortest path, and v is a node on that path, then also the paths from s to v and from v to t are shortest paths.

Simple argument: If there was a shorter path w‘ from s to v, we could shorten the shortest path from s to t.

(53)

29.05.12 | Komplexität | 119

Shortest Path Algorithms

Dijkstra‘s Algorithm

•  Preliminaries

•  directed graph G=(V,E), all edge weights positiv

•  G is represented with the help of adjacency lists

•  start node s

•  S: set of nodes where the real distance to s is already known of

•  dist[v] distance estimation of v

•  π[v] predecessor node of v. After all, we can read the desired path from π.

•  set A: A := V \ S

(54)

29.05.12 | Komplexität | 120

Shortest Path Algorithms

Dijkstra‘s Algorithm

Initialize(G,s) // for all nodes v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

S := ∅; A := V;

while A ≠ ∅ do

u := argmin{ dist[a] | a∈A }; A := A \ {u};

S := S ∪ {u};

for each node v ∈ Adj[u] do if dist[v] > dist[u] + f(u,v) then dist[v] := dist[u] + f(u,v);

π[v] := u;

s S

x u

(55)

29.05.12 | Komplexität | 121

Shortest Path Algorithms

Dijkstra‘s Algorithm

1: Initialize(G,s) // for all nodes v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

2: S := ∅; 3: A := V;

4: while A ≠ ∅ do

5: u := argmin{ dist[a] | a∈A }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v ∈ Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s

x y

z v

0

5 2 10

∞ ∞

∞ ∞

3 4 6

2 7 9 1

A π[v]=u S

u concerning line 5

(56)

29.05.12 | Komplexität | 122

Shortest Path Algorithms

Dijkstra‘s Algorithm

1: Initialize(G,s) // for all nodes v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

2: S := ∅; 3: A := V;

4: while A ≠ ∅ do

5: u := argmin{ dist[a] | a∈A }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v ∈ Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s

x y

z v

0

5 2 10

5 ∞

10 ∞

3 4 6

2 7 9 1

A π[v]=u S

u concerning line 5

(57)

29.05.12 | Komplexität | 123

Shortest Path Algorithms

Dijkstra‘s Algorithm

1: Initialize(G,s) // for all nodes v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

2: S := ∅; 3: A := V;

4: while A ≠ ∅ do

5: u := argmin{ dist[a] | a∈A }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v ∈ Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s

x y

z v

0

5 2 10

5 7

8 14

3 4 6

2 7 9 1

A π[v]=u S

u concerning line 5

(58)

29.05.12 | Komplexität | 124

Shortest Path Algorithms

Dijkstra‘s Algorithm

1: Initialize(G,s) // for all nodes v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

2: S := ∅; 3: A := V;

4: while A ≠ ∅ do

5: u := argmin{ dist[a] | a∈A }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v ∈ Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s

x y

z v

0

5 2 10

5 7

8 13

3 4 6

2 7 9 1

A π[v]=u S

u concerning line 5

(59)

29.05.12 | Komplexität | 125

Shortest Path Algorithms

Dijkstra‘s Algorithm

1: Initialize(G,s) // for all nodes v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

2: S := ∅; 3: A := V;

4: while A ≠ ∅ do

5: u := argmin{ dist[a] | a∈A }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v ∈ Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s

x y

z v

0

5 2 10

5 7

8 9

3 4 6

2 7 9 1

A π[v]=u S

u concerning line 5

(60)

29.05.12 | Komplexität | 126

Shortest Path Algorithms

Dijkstra‘s Algorithm

1: Initialize(G,s) // for all nodes v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

2: S := ∅; 3: A := V;

4: while A ≠ ∅ do

5: u := argmin{ dist[a] | a∈A }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v ∈ Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s

x y

z v

0

5 2 10

7

8 9

3 4 6

2 7 9 1

5 A

π[v]=u S

u concerning line 5

(61)

29.05.12 | Komplexität | 127

Shortest Path Algorithms

Dijkstra‘s Algorithm

1: Initialize(G,s) // for all nodes v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

2: S := ∅; 3: A := V;

4: while A ≠ ∅ do

5: u := argmin{ dist[a] | a∈A }; A := A \ {u};

6: S := S ∪ {u};

7: for each node v ∈ Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

Runtime: O(|E|⋅O(Zeile 9) + |V|⋅O(Zeile 5)) With simple idea: O(|E| + |V|⋅|V|) = O(|V|2)

(62)

Dijkstra‘s Algorithm, Correctness

•  Lemma Dijk1: dist[v] ≥ δ(s,v)

Prf. Using induction over the number of executions of lines 8 a. 9.

•  ISta: At beginning: dist[s] = 0 and dist[u] = ∞ for all other u.

•  IH: Claim is valid up to k-th exe- cution of lines 8 and 9.

•  ISte: Let v be the first node for that the claim is wrong, generated by

the (k+1)-st execution of lines 8 .a. 9.

Then, after the (k+1)-st execution:

dist[u]+f(u,v) = dist[v] < δ(s,v) [concerning assumption]. And it is δ(s,v) ≤ δ(s,u) + f(u,v) [shortest path property]

Thus, in total dist[u]+f(u,v) < δ(s,u) + f(u,v). But, because dist[u] in (k+1)-st execution has not been changed at all, already before was dist[u]< δ(s,u).

→ Assumption dist[v] < δ(s,v) for v was wrong.

29.05.12 | Komplexität | 128

Shortest Path Algorithms

Dijkstra‘s Algorithm 1: Initialize(G,s) 2: S := ;

3: A := V;

4: while A ≠ do

5: u := argmin{ dist[a] | aA }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

(63)

29.05.12 | Komplexität | 129

Shortest Path Algorithms

Dijkstra‘s Algorithm, Correctness

Claim DijkKor: Running Dijksta‘s Algorithm on a weighted directed graph with non- negativ weight function w and startnode s results in u∈S: dist[u] = δ(s,u) for all nodes, when the algorithm has finished its work.

Proof via contradiction: Let u be the first node for that it is dist[u] ≠ δ(s,u), when it is added to S. [We will be successful, if we can show that for this first u it is

nonetheless true that dist[u] = δ(s,u).]

Now:

•  u≠s, then dist[s] is correctly set and never again changed.

•  because of u≠s, directly before adding u it is S≠∅

•  additionally, there must exist a path from s to u (as otherwise: dist[u] = δ(s,u)=∞)

•  there is a shortest path p from s to u.

(64)

29.05.12 | Komplexität | 130

Shortest Path Algorithms

Dijkstra‘s Algorithm, Correctness

•  p connects node s in S with another node u in A (=V \ S). Let y be the first node along p such that y∈V\S and let x be the predecessor of y. p can be devided into in p1 and p2, such that p1 is completely contained in S.

s S

x

y

u

We concentrate our attention on the moment when u is added to S!

For y as well as for u it is valid: y,uA

(65)

Dijkstra‘s Algorithm, Correctness

•  Lemma Dijk2: dist[y] ≤ δ(s,y) Proof:.

x∈S and y is in the shortest path in front of u. This implies that y was set to its final value with the help of lines 8 a. 9, before (i.e. when x went to S) this

happened to u.

Therefore: dist[y] ≤ dist[x] + f(x,y) [because of lines 8 a. 9]

= δ(s,x) + f(x,y) [because x∈S and assumption in DijkKor]

= δ(s,y), because s → x → y is shortest path. ✔

29.05.12 | Komplexität | 131

Shortest Path Algorithms

Dijkstra‘s Algorithm 1: Initialize(G,s) 2: S := ;

3: A := V;

4: while A ≠ do

5: u := argmin{ dist[a] | aA }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s S

x y

u

(66)

Dijkstra‘s Algorithm, Correctness

•  Lemma Dijk3: With lemmata

Dijk1 and Dijk2 we know for the given situation:

In the moment, when u goes into S, it is valid that dist[y] = δ(s,y).

29.05.12 | Komplexität | 132

Shortest Path Algorithms

Dijkstra‘s Algorithm 1: Initialize(G,s) 2: S := ;

3: A := V;

4: while A ≠ do

5: u := argmin{ dist[a] | aA }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s S

x y

u

(67)

Dijkstra‘s Algorithm, Correctness

•  We conclude:

dist[y] = δ(s,y) ≤ δ(s,u) ≤ dist[u] and dist[u] ≤ dist[y] because of line 5 and y,u∈A

Thus, alltogether: dist[u] ≤ dist[y] ≤ δ(s,u) ≤ dist[u],

also dist[u] = δ(s,u) → Contradiction to assupmtion the claim DijkKor

29.05.12 | Komplexität | 133

Shortest Path Algorithms

Dijkstra‘s Algorithm 1: Initialize(G,s) 2: S := ;

3: A := V;

4: while A ≠ do

5: u := argmin{ dist[a] | aA }; A:= A \ {u}

6: S := S ∪ {u};

7: for each node v Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

s S

x y

u

shortest path Lemma Dijk1

(68)

29.05.12 | Komplexität | 134

Shortest Path Algorithms

Improvement for Dijkstra‘s Algorithmu

1: Initialize(G,s) // für alle Knoten v≠s: π[v]:=nil; dist[v]:=∞; dist[s]:=0;π[s]:=nil;

2: S := ∅; 3: A := V;

4: while A ≠ ∅ do

5: u := argmin{ dist[a] | a∈A } 6: S := S ∪ {u};

7: for each node v ∈ Adj[u] do 8: if dist[v] > dist[u] + f(u,v) then 9: dist[v] := dist[u] + f(u,v);

10: π[v] := u;

Runtime: O(|E|⋅O(Zeile 9) + |V|⋅O(Zeile 5)) = O(|V|2)

With the help of heaps (new abstract data type similar to queue or stack), it can be achieved : O(|E|⋅log(|V|) + |V|⋅log(|V|)) and even O(|E| + |V|⋅log(|V|)

Referenzen

ÄHNLICHE DOKUMENTE

Die Aufwendungen für den ehrenamtlichen Stadtbürgermeister, die Beigeordneten, die ehrenamtlichen Rats- und Ausschussmit- glieder sowie die Aufwendungen für Bauhofmitarbeiter,

Die vom Imperialismus unterstützte Offensive der Reaktion gegen Ihre Par- tei und gegen die Grundlagen der Gesellschafts- ordnung der CSSR birgt nach unserer festen Über- zeugung

(FHs werden allerdings weiterhin Dipl.Ing.s verleihen). Vorteil dieser curcgeIung wird sicher die intemaäonale VergIcichbarkeit der Abschlüsse sein. Dabei wird das Masu:r-Studium

Durch eine enge Zusammenarbeit und Erziehungspartner- schaft von Eltern und Schule schaffen wir eine gute Grundlage für die Persönlichkeitsentwicklung. Das zeigt sich auch

Oder ist die Information, dass der ATSV Ha- benhausen seit kurzem Reha-Sport anbietet, noch nicht bei Euch angekommen. Denn die überfüllten Sprechzimmer bei den Orthopäden

Juli 1991 koordinierten Gesetze über die Staatsbuchführung gegen die Artikel 10 und 11 der Verfassung, soweit kraft dieser Bestimmungen eine Schuldforderung zu Lasten des Staates,

Ebenso sind den Personalentwicklungsberaterinnen und -beratern Fälle gemeldet worden, in denen den Betroffenen schriftlich mitgeteilt wurde, dass sie der PVS gemeldet werden

Der Katalog wassergefährdender Stoffe erfaßt eine Reihe von Stoffen, die vornehmlich im Verkehr sind und ausschließlich durch die KBwS eingestuft worden sind.. Die Stoffe sind