Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Depth-First Traversal and Connected Components

Depth-First Traversal and Connected Components

The main idea of Depth First Search is to start with an unvisited vertex as the starting vertex, and follow the edges of the current vertex to reach unvisited vertices. When there are no unvisited vertices, backtrack to the previous vertex and continue exploring other vertices, until all vertices have been visited.

The traversal order of the example graph below starting from 0 is shown on the right:

A maximal connected subgraph of an undirected graph G is called a connected component (or connected branch) of G. A connected graph has only one connected component, namely itself; a disconnected undirected graph has multiple connected components. There are no edges connecting different connected components. Depth-first traversal can be used to find connected components.

Below, we implement depth-first traversal of a graph using finding connected components as an example, called dfs. In the code snippet, the visited array records whether a node has been visited during dfs, ccount records the number of connected components, the id array represents the connected component label corresponding to each node, and two nodes with the same id value belong to the same connected component.

...

// Constructor, compute the connected components of an unweighted graph

public

Components

(

Graph graph

)

{

   

// Algorithm initialization

    G

=

graph

;

    visited

=

new

boolean

[

G.

V

(

)

]

;

    id

=

new

int

[

G.

V

(

)

]

;

    ccount

=

0

;

   

for

(

int

i

=

0

;

i

<

G.

V

(

)

;

i

++

)

{

        visited

[

i

]

=

false

;

        id

[

i

]

=

1

;

   

}

   

// Find the connected components of the graph

   

for

(

int

i

=

0

;

i

<

G.

V

(

)

;

i

++

)

       

if

(

!

visited

[

i

]

)

{

            dfs

(

i

)

;

            ccount

++;

       

}

}

...

Depth-first traversal of a graph is a recursive process; implementation code:

...

// Depth-first traversal of the graph

void

dfs

(

int

v

)

{

    visited

[

v

]

=

true

;

    id

[

v

]

=

ccount

;

   

for

(

int

i

:

G.

adj

(

v

)

)

{

       

if

(

!

visited

[

i

]

)

            dfs

(

i

)

;

   

}

}

...

Java Example Code

Source code package download:Download

src/runoob/graph/Components.java file code:

package

runoob.graph

;

import

runoob.graph.read.Graph

;

/**
 * Depth-first traversal
 */

public

class

Components

{

    Graph G

;

                   

// Reference to the graph

   

private

boolean

[

]

visited

;

 

// Record whether a node is visited during dfs

   

private

int

ccount

;

       

// Record the number of connected components

   

private

int

[

]

id

;

         

// The connected component label corresponding to each node

   

// Depth-first traversal of the graph

   

void

dfs

(

int

v

)

{

        visited

[

v

]

=

true

;

        id

[

v

]

=

ccount

;

       

for

(

int

i

:

G.

adj

(

v

)

)

{

           

if

(

!

visited

[

i

]

)

                dfs

(

i

)

;

       

}

   

}

   

// Constructor, compute the connected components of an unweighted graph

   

public

Components

(

Graph graph

)

{

       

// Algorithm initialization

        G

=

graph

;

        visited

=

new

boolean

[

G.

V

(

)

]

;

        id

=

new

int

[

G.

V

(

)

]

;

        ccount

=

0

;

       

for

(

int

i

=

0

;

i

<

G.

V

(

)

;

i

++

)

{

            visited

[

i

]

=

false

;

            id

[

i

]

=

1

;

       

}

       

// Find the connected components of the graph

       

for

(

int

i

=

0

;

i

<

G.

V

(

)

;

i

++

)

           

if

(

!

visited

[

i

]

)

{

                dfs

(

i

)

;

                ccount

++;

           

}

   

}

   

// Return the number of connected components of the graph

   

int

count

(

)

{

       

return

ccount

;

   

}

   

// Query whether vertex v and vertex w are connected

   

boolean

isConnected

(

int

v ,

int

w

)

{

       

assert

v

=

0

&&

v

<

G.

V

(

)

;

       

assert

w

=

0

&&

w

<

G.

V

(

)

;

       

return

id

[

v

]

==

id

[

w

]

;

   

}

}