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

Graph Theory Fundamentals and Representation

Graph Theory Fundamentals and Representation

I. Concept and Introduction

Graph Theory is a branch of discrete mathematics, a field of study concerned with graphs.

A graph is a mathematical structure used to model pairwise relationships between objects, consisting of "nodes" or "vertices" (Vertex) and "edges" (Edge) that connect these vertices.

It is worth noting that the vertex set of a graph cannot be empty, but the edge set can be. A graph may be undirected, meaning its edges do not need to distinguish direction when connecting vertices. Otherwise, the graph is called directed. The left figure below is a typical undirected graph structure, while the right figure is a directed graph. All graphs introduced in this chapter are undirected.

Classification of graphs: unweighted and weighted graphs, depending on whether the edges connecting nodes have numerical values associated with them. If so, it is a weighted graph; otherwise, it is an unweighted graph.

**Connectivity of graphs:** In graph theory, a connected graph is based on the concept of connectivity. In an undirected graph G, if there is a path from vertex i to vertex j (and of course a path from j to i), then i and j are said to be connected. If G is a directed graph, then all edges in the path connecting i and j must be in the same direction. If any two points in the graph are connected, the graph is called a connected graph. If the graph is directed, it is called a strongly connected graph (note: paths must exist in both directions). Connectivity is a fundamental property of graphs.

**Complete graph:** A complete graph is a simple undirected graph in which every pair of distinct vertices is connected by exactly one edge.

**Self-loop edge:** An edge whose start and end are the same vertex.

**Parallel edges:** Multiple edges connecting the same two vertices.

II. Applicability Notes

Graphs can be used to model many types of relationships and processes in physical, biological, social, and information systems, and many practical problems can be represented by graphs. Therefore, graph theory has become a powerful mathematical tool for many disciplines such as operations research, cybernetics, information theory, network theory, game theory, physics, chemistry, biology, social sciences, linguistics, and computer science. When emphasizing their application to real-world systems, networks are sometimes defined as a graph in which relationships between attributes (such as names) are associated in the form of nodes and/or edges.

III. Representation of graphs

**Adjacency matrix:** 1 indicates connected, 0 indicates not connected.

**Adjacency list:** Only expresses information about vertices connected to a vertex

Adjacency lists are suitable for representing sparse graphs (Sparse Graph)

Adjacency matrices are suitable for representing dense graphs (Dense Graph)

Java Example Code

Source code package download:Download

(1) Adjacency matrix

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

package

runoob.graph

;

/**
 * Adjacency matrix
 */

public

class

DenseGraph

{

   

// 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

DenseGraph

(

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

]

;

   

}

}

(2) Adjacency list

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

package

runoob.graph

;

import

java.util.Vector

;

/**
 * Adjacency list
 */

public

class

SparseGraph

{

   

// 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

Vector

<

Integer

[

]

g

;

   

// Constructor

   

public

SparseGraph

(

int

n ,

boolean

directed

)

{

       

assert

n

=

0

;

       

this

.

n

=

n

;

       

this

.

m

=

0

;

 

       

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

(

)

;

   

}

   

// 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

;

        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

;

   

}

}