Sie sind auf Seite 1von 10

Graph Traversal

Finding connected components

R. Inkulu
http://www.iitg.ac.in/rinkulu/

(Finding connected components)

1/6

Definition
111
000
000000001111111111
00010 7
111
0000000000
00011111111
111
000000000
111111111
00000000
11111111
000000000010
1111111111
000000000
111111111
00000000
11111111
0000000000
1111111111
1010
000000000
111111111
3
00000000
11111111
0000000000
1111111111
000
111
000
111
000000000
111111111
2
00000000
11111111
0000000000
1111111111
000000000
111111111
000
111
101
000
111
00000000
11111111
000000000
1111111111111111
0000000000000000
00000000000
000000000111111111
111111111
010
0000000011111111111
00000000000
11111111111
00000000011111111
111111111
000000000
111111111
00000000
11111111
00000000000
11111111111
000000000
111111111
000000000
111111111
10
000
111
00000000
11111111
00000000000
11111111111
000000000
111111111
000
111
000000000
111111111
000
111
1011111111
00010 8
111
00000000
00000000000
11111111111
000000000
111111111
000
111
000000000
111111111
4 111
000
0
1
00000000
111111111111111111111111
0000000000000000
1010 5
10
10
000
111
00010 6
111
1111
000

111
000
9
000
111
1010
101
010
1010
10 10
000
111
1010
000
111

111
000
11
000
111
10
1010
10
1010
101011111111
000
111
12
00000000
00000000
11111111
000
111
00000000
11111111
00000000
11111111
00000000
11111111
00000000
11111111
00000000
0000000011111111
11111111
00000000
11111111
00000000
11111111
000
111
000
111
00000000
11111111
00000000
11111111
14111
000
000 13
111
00000000
0000000011111111
11111111
111111111111111
000000000000000

A connected component of a given undirected graph G(V, E) is a maximal


subgraph G (V , E ) of G such that for any two vertices vi and vj in G there
exists a path between vi and vj in G .
If two nodes s and t are connected, then their connected components are

identical; otherwise, they are disjoint.


(Finding connected components)

2/6

Incremental expansion to include more and more


vertices

000
111
1111
000
1010 7
1111111111
0000000000
000
111
0000000000
1111111111
00011111111
111
0000000000
1111111111
00000000
0000000000
1111111111
1010
0000000000
1111111111
00000000
11111111
0000000000
1111111111
0000000000
1111111111
3
00000000
11111111
0000000000
1111111111
000
111
000
0000000000
1111111111
2111
11111111111111111
00000000000000000
00000000000
11111111111
00000000
00000000001010
1111111111
000
111
000
111
00000000
11111111
000000000011111111
1111111111
00000000000
11111111111
00000000
11111111
1010
00000000
11111111
00000000000
11111111111
0000000000
1111111111
00000000
11111111
00000000
11111111
00000000000
11111111111
0000000000
1111111111
00000000
11111111
1010 8
000
111
00000000
11111111
00000000000
0000000000
1111111111
000
111
00000000
11111111
000
111
1010 11111111111
000
111
00000000
11111111
00000000000
11111111111
0000000000
1111111111
000
111
00000000
11111111
4 111
00000000000000000000
5
1111111111111111111111111
10100000000
1010
000
111
00010 6
111

111
000
000
111
1010 9
1010
1010
1010
000
111
1010 10
000
111

111
000
00010 11
111
1010
1010
1010
000
111
10 12
00011111111
111
00000000
00000000
11111111
00000000
11111111
00000000
11111111
00000000
11111111
00000000
11111111
00000000
11111111
00000000
11111111
000
111
000
111
00000000
11111111
00000000
11111111
14111
000
000 13
111
00000000
0000000011111111
11111111
111111111111111
000000000000000

Up till reaching maximality

(Finding connected components)

3/6

Finding another connected component

000
111
1111
000
1111111111
0000000000
00010 7
111
000000000
111111111
00011111111
111
0000000000
1111111111
00000000
000000000
111111111
0000000000
1111111111
000000003111111111
11111111
0000000001010
0000000000111111111
1111111111
00000000
11111111
000000000
111111111
000
111
000
111
000000000
0000000000
1111111111
2 111111111
00000000
11111111
000000000
0000000001010
111111111
000
111
000
111
000000000
111111111
0000000000
1111111111
1111111111111111
0000000000000000
0000000000
1111111111
000000000
111111111
000000000
111111111
1010
000000000
111111111
0000000000
1111111111
000000000
111111111
000000000
111111111
000000000
0000000000
1111111111
000000000111111111
000000000111111111
111111111
000
111
000000000
111111111
0000000000
1111111111
000000000
111111111
000
111
000000000
111111111
000
111
10111111111
0001010 8
111
000000000
1111111111111111
0000000000000000
0000000000
1111111111
000000000
111111111
000
111
000000000
111111111
4 111
000
10111111111
000000000
10 5
1010
10
000
111
10 6
000
111

111
000
00010 9
111
101
01
010
101
010
000
111
0001010 10
111

111
000
0001010 11
111
10
1010
1010
0110 12
000
111
0000000
1111111
00011111111
111
00000000
0000000
1111111
00000000
11111111
0000000
1111111
00000000
000000011111111
1111111
00000000
11111111
0000000
1111111
000
111
000 13
111
00000000
11111111
0000000
1111111
11111111111111
00000000000000
14111
0001111111
000
111
00000000
000000011111111

Starting from a node which is not part of any component yet.

(Finding connected components)

4/6

Correctness of incremental expansion


11110 7
000
00010
111
000000000
111111111
00011111111
111
0000000000
1111111111
00000000
000000000
111111111
10
0000000000
1111111111
00000000
11111111
000000000
0000000000
1111111111
3111111111
00000000
11111111
0000000001010
111111111
000
111
000
0000000000
1111111111
1111111111111111
0000000000000000
000000000
111111111
2111
00000000
11111111
000000000
111111111
000111111111
111
1010
000
111
000000000
111111111
0000000000
1111111111
000000000
000000000
111111111
00000000
11111111
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
1010
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
000
111
000000000
111111111
8
000000000
111111111
000000000
111111111
000
111
00000000
11111111
000
111
0
1
000
111
000000000
111111111
00000000010
111111111
000000000
111111111
000
111
00000000
4 111
00011111111
10111111111
000000000
5
1111111111111111
0000000000000000
1010
1010
000
111
10 6
000
111
1111
000

(Finding connected components)

111 9
000
000
111
10
1010
1010
1010
0110 10
000
111
000
111
10

11110 11
000
00010
111
1010
1001
0110
000
111
10 12
000
111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
000
111
000
111
00000000
11111111
0000000
1111111
14111
0000000000000000
000 13
111
00000000
000000011111111
1111111
1111111111111

5/6

Correctness of incremental expansion


11110 7
000
00010
111
000000000
111111111
00011111111
111
0000000000
1111111111
00000000
000000000
111111111
10
0000000000
1111111111
00000000
11111111
000000000
0000000000
1111111111
3111111111
00000000
11111111
0000000001010
111111111
000
111
000
0000000000
1111111111
1111111111111111
0000000000000000
000000000
111111111
2111
00000000
11111111
000000000
111111111
000111111111
111
1010
000
111
000000000
111111111
0000000000
1111111111
000000000
000000000
111111111
00000000
11111111
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
1010
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
000
111
000000000
111111111
8
000000000
111111111
000000000
111111111
000
111
00000000
11111111
000
111
0
1
000
111
000000000
111111111
00000000010
111111111
000000000
111111111
000
111
00000000
4 111
00011111111
10111111111
000000000
5
1111111111111111
0000000000000000
1010
1010
000
111
10 6
000
111
1111
000

111 9
000
000
111
10
1010
1010
1010
0110 10
000
111
000
111
10

11110 11
000
00010
111
1010
1001
0110
000
111
10 12
000
111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
000
111
000
111
00000000
11111111
0000000
1111111
14111
0000000000000000
000 13
111
00000000
000000011111111
1111111
1111111111111

Each subgraph G (V , E ) produced by the above stated algorithm is a


connected component:

(Finding connected components)

5/6

Correctness of incremental expansion


11110 7
000
00010
111
000000000
111111111
00011111111
111
0000000000
1111111111
00000000
000000000
111111111
10
0000000000
1111111111
00000000
11111111
000000000
0000000000
1111111111
3111111111
00000000
11111111
0000000001010
111111111
000
111
000
0000000000
1111111111
1111111111111111
0000000000000000
000000000
111111111
2111
00000000
11111111
000000000
111111111
000111111111
111
1010
000
111
000000000
111111111
0000000000
1111111111
000000000
000000000
111111111
00000000
11111111
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
1010
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
000
111
000000000
111111111
8
000000000
111111111
000000000
111111111
000
111
00000000
11111111
000
111
0
1
000
111
000000000
111111111
00000000010
111111111
000000000
111111111
000
111
00000000
4 111
00011111111
10111111111
000000000
5
1111111111111111
0000000000000000
1010
1010
000
111
10 6
000
111
1111
000

111 9
000
000
111
10
1010
1010
1010
0110 10
000
111
000
111
10

11110 11
000
00010
111
1010
1001
0110
000
111
10 12
000
111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
000
111
000
111
00000000
11111111
0000000
1111111
14111
0000000000000000
000 13
111
00000000
000000011111111
1111111
1111111111111

Each subgraph G (V , E ) produced by the above stated algorithm is a


connected component:
There exists a path between any two vertices of G .

(Finding connected components)

5/6

Correctness of incremental expansion


11110 7
000
00010
111
000000000
111111111
00011111111
111
0000000000
1111111111
00000000
000000000
111111111
10
0000000000
1111111111
00000000
11111111
000000000
0000000000
1111111111
3111111111
00000000
11111111
0000000001010
111111111
000
111
000
0000000000
1111111111
1111111111111111
0000000000000000
000000000
111111111
2111
00000000
11111111
000000000
111111111
000111111111
111
1010
000
111
000000000
111111111
0000000000
1111111111
000000000
000000000
111111111
00000000
11111111
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
1010
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
000
111
000000000
111111111
8
000000000
111111111
000000000
111111111
000
111
00000000
11111111
000
111
0
1
000
111
000000000
111111111
00000000010
111111111
000000000
111111111
000
111
00000000
4 111
00011111111
10111111111
000000000
5
1111111111111111
0000000000000000
1010
1010
000
111
10 6
000
111
1111
000

111 9
000
000
111
10
1010
1010
1010
0110 10
000
111
000
111
10

11110 11
000
00010
111
1010
1001
0110
000
111
10 12
000
111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
000
111
000
111
00000000
11111111
0000000
1111111
14111
0000000000000000
000 13
111
00000000
000000011111111
1111111
1111111111111

Each subgraph G (V , E ) produced by the above stated algorithm is a


connected component:
There exists a path between any two vertices of G .
G is maximal w.r.t. both vertices and edges.

(Finding connected components)

5/6

Correctness of incremental expansion


11110 7
000
00010
111
000000000
111111111
00011111111
111
0000000000
1111111111
00000000
000000000
111111111
10
0000000000
1111111111
00000000
11111111
000000000
0000000000
1111111111
3111111111
00000000
11111111
0000000001010
111111111
000
111
000
0000000000
1111111111
1111111111111111
0000000000000000
000000000
111111111
2111
00000000
11111111
000000000
111111111
000111111111
111
1010
000
111
000000000
111111111
0000000000
1111111111
000000000
000000000
111111111
00000000
11111111
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
1010
000000000
111111111
000000000
111111111
000000000
111111111
00000000
11111111
000
111
000000000
111111111
8
000000000
111111111
000000000
111111111
000
111
00000000
11111111
000
111
0
1
000
111
000000000
111111111
00000000010
111111111
000000000
111111111
000
111
00000000
4 111
00011111111
10111111111
000000000
5
1111111111111111
0000000000000000
1010
1010
000
111
10 6
000
111
1111
000

111 9
000
000
111
10
1010
1010
1010
0110 10
000
111
000
111
10

11110 11
000
00010
111
1010
1001
0110
000
111
10 12
000
111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
00000000
11111111
0000000
1111111
000
111
000
111
00000000
11111111
0000000
1111111
14111
0000000000000000
000 13
111
00000000
000000011111111
1111111
1111111111111

Each subgraph G (V , E ) produced by the above stated algorithm is a


connected component:
There exists a path between any two vertices of G .
G is maximal w.r.t. both vertices and edges.
Every vertex/edge belongs to some connected component.
(Finding connected components)

5/6

Concrete Algorithm

Using either of:


Breadth First Traversal
Depth First Traversal

(Finding connected components)

6/6

Das könnte Ihnen auch gefallen