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

Adjacent Node Iterator

Adjacent Node Iterator

The most common operation in graph theory is traversing adjacent edges, going through the relevant adjacent edges via a vertex. Traversing adjacent edges in an adjacency matrix has time complexity O(V), while an adjacency list allows direct lookup and is more efficient.

Adjacency matrix iteration:

...

public

Iterable

<

Integer

adj

(

int

v

)

{

assert

v

=

0

&&

v

<

n

;

    Vector

<

Integer

adjV

=

new

Vector

<

Integer

(

)

;

   

for

(

int

i

=

0

;

i

<

n

;

i

++

)

       

if

(

g

[

v

]

[

i

]

)

            adjV.

add

(

i

)

;

   

return

adjV

;

}

...

Adjacency list iteration:

...

// Return all adjacent edges of a vertex in the graph

// Since Java uses reference semantics, returning a Vector incurs no extra overhead,

public

Iterable

<

Integer

adj

(

int

v

)

{

assert

v

=

0

&&

v

<

n

;

   

return

g

[

v

]

;

}

...

For these two graph representations we can abstract an interface to generate a framework for this set of algorithms, without worrying about whether the underlying representation is an adjacency list or adjacency matrix.

This section writes a test case GraphReadTest that implements graph display by calling the abstract interface; you can view it in the read package.

/**
 * Abstract interface for graphs
 */

public

interface

Graph

{

   

public

int

V

(

)

;

   

public

int

E

(

)

;

   

public

void

addEdge

(

int

v ,

int

w

)

;

   

boolean

hasEdge

(

int

v ,

int

w

)

;

   

void

show

(

)

;

   

public

Iterable

<

Integer

adj

(

int

v

)

;

}

Java Example Code

Source code package download:Download

(1) Adjacency matrix iteration

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

package

runoob.graph

;

import

java.util.Vector

;

/**
 * Adjacency matrix iteration
 */

public

class

DenseGraphIterater

{

   

// Number of nodes

   

private

int

n

;

   

// Number of edges

   

private

int

m

;

   

// Whether it is a directed graph

   

private

boolean

directed

;

   

// The specific data of the graph

   

private

boolean

[

]

[

]

g

;

   

// Constructor

   

public

DenseGraphIterater

(

int

n ,

boolean

directed

)

{

       

assert

n

=

0

;

       

this

.

n

=

n

;

       

this

.

m

=

0

;

       

this

.

directed

=

directed

;

       

// g is initialized as an n*n boolean matrix, each g[i][j] is false, indicating no edge

       

// false is the default value of a boolean variable

        g

=

new

boolean

[

n

]

[

n

]

;

   

}

   

// Return the number of nodes

   

public

int

V

(

)

{

return

n

;

}

   

// Return the number of edges

   

public

int

E

(

)

{

return

m

;

}

   

// Add an edge to the graph

   

public

void

addEdge

(

int

v ,

int

w

)

{

       

assert

v

=

0

&&

v

<

n

;

       

assert

w

=

0

&&

w

<

n

;

       

if

(

hasEdge

(

v , w

)

)

           

return

;

        g

[

v

]

[

w

]

=

true

;

       

if

(

!

directed

)

            g

[

w

]

[

v

]

=

true

;

        m

++;

   

}

   

// Verify whether there is an edge from v to w in the graph

   

boolean

hasEdge

(

int

v ,

int

w

)

{

       

assert

v

=

0

&&

v

<

n

;

       

assert

w

=

0

&&

w

<

n

;

       

return

g

[

v

]

[

w

]

;

   

}

   

// Return all adjacent edges of a vertex in the graph

   

// Since Java uses reference semantics, returning a Vector incurs no extra overhead,

   

public

Iterable

<

Integer

adj

(

int

v

)

{

assert

v

=

0

&&

v

<

n

;

        Vector

<

Integer

adjV

=

new

Vector

<

Integer

(

)

;

       

for

(

int

i

=

0

;

i

<

n

;

i

++

)

           

if

(

g

[

v

]

[

i

]

)

                adjV.

add

(

i

)

;

       

return

adjV

;

   

}

}

(2) Adjacency list iteration

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

package

runoob.graph

;

import

java.util.Vector

;

/**
 * Adjacency list iteration
 */

public

class

SparseGraphIterater

{

   

private

int

n

;

 

// Number of nodes

   

private

int

m

;

 

// Number of edges

   

private

boolean

directed

;

   

// Whether it is a directed graph

   

private

Vector

<

Integer

[

]

g

;

// The specific data of the graph

   

// Constructor

   

public

SparseGraphIterater

(

int

n ,

boolean

directed

)

{

       

assert

n

=

0

;

       

this

.

n

=

n

;

       

this

.

m

=

0

;

   

// Initialize with no edges

       

this

.

directed

=

directed

;

       

// g is initialized as n empty vectors, meaning each g[i] is empty, i.e., no edge

        g

=

(

Vector

<

Integer

[

]

)

new

Vector

[

n

]

;

       

for

(

int

i

=

0

;

i

<

n

;

i

++

)

            g

[

i

]

=

new

Vector

<

Integer

(

)

;

   

}

   

public

int

V

(

)

{

return

n

;

}

// Return the number of nodes

   

public

int

E

(

)

{

return

m

;

}

// Return the number of edges

   

// Add an edge to the graph

   

public

void

addEdge

(

int

v,

int

w

)

{

       

assert

v

=

0

&&

v

<

n

;

       

assert

w

=

0

&&

w

<

n

;

        g

[

v

]

.

add

(

w

)

;

       

if

(

v

!=

w

&&

!

directed

)

            g

[

w

]

.

add

(

v

)

;

        m

++;

   

}

   

// Verify whether there is an edge from v to w in the graph

   

boolean

hasEdge

(

int

v ,

int

w

)

{

       

assert

v

=

0

&&

v

<

n

;

       

assert

w

=

0

&&

w

<

n

;

       

for

(

int

i

=

0

;

i

<

g

[

v

]

.

size

(

)

;

i

++

)

           

if

(

g

[

v

]

.

elementAt

(

i

)

==

w

)

               

return

true

;

       

return

false

;

   

}

   

// Return all adjacent edges of a vertex in the graph

   

// Since Java uses reference semantics, returning a Vector incurs no extra overhead,

   

public

Iterable

<

Integer

adj

(

int

v

)

{

assert

v

=

0

&&

v

<

n

;

       

return

g

[

v

]

;

   

}

}