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

Data Structures - Graph

Data Structures - Graph

A graph (Graph) is a more general non-linear data structure used to represent "many-to-many" complex relationships between elements, composed of vertices and the edges connecting them.


Basic concepts of graphs

Types of graphs - undirected graph, directed graph, weighted graph

Undirected graph

0

—

1

Edges have no direction; connections are symmetric

Example: social network friendships

Directed graph

0

→

1

Edges have direction; one-way connection

Example: follow relationships, web page links

Weighted graph

0

5

1

Edges carry weights (distance/cost)

Example: map navigation, network latency


Graph storage methods

Adjacency matrix vs adjacency list

Adjacency Matrix

Space complexity

O(V²)

Determine if two vertices are connected

O(1)

Traverse adjacent vertices

O(V)

Suitable for: dense graphs · int graph[V][V]

Adjacency list

Space complexity

O(V+E)

Determine if two vertices are connected

O(degree)

Traverse adjacent vertices

O(degree)

Suitable for: sparse graphs · linked-list array


Graph traversal

DFS vs BFS - traversal starting from vertex 0

DFS depth-first

0

→

1

→

3

→

4

→

2

→

5

Follow a path to its end then backtrack

Implementation: recursion / stack | suitable for path search, topological sort

BFS breadth-first

0

→

1

→

2

→

3

→

4

→

5

Expand layer by layer; visit nodes in the same layer first

Implementation: queue | naturally suited for shortest paths in unweighted graphs

Example

#include

#include

#define V 6 /* number of vertices */

/* graph represented by an adjacency matrix */

int

graph

[

V

]

[

V

]

=

{

   

{

0

,

1

,

1

,

0

,

0

,

0

}

,

 

/* adjacency of vertex 0 */

   

{

1

,

0

,

0

,

1

,

1

,

0

}

,

 

/* vertex 1 */

   

{

1

,

0

,

0

,

0

,

1

,

0

}

,

 

/* vertex 2 */

   

{

0

,

1

,

0

,

0

,

0

,

1

}

,

 

/* vertex 3 */

   

{

0

,

1

,

1

,

0

,

0

,

1

}

,

 

/* vertex 4 */

   

{

0

,

0

,

0

,

1

,

1

,

0

}

 

/* vertex 5 */

}

;

bool visited

[

V

]

;

/* DFS: depth-first search, recursive implementation
   characteristics: follow a path to its end then backtrack; suitable for path search and connectivity checking */

void

DFS

(

int

v

)

{

    visited

[

v

]

=

true

;

   

printf

(

"%d "

,

v

)

;

       

/* visit the current vertex */

   

for

(

int

i

=

0

;

i

<

V

;

i

++

)

{

       

if

(

graph

[

v

]

[

i

]

&&

!

visited

[

i

]

)

{

            DFS

(

i

)

;

 

/* recursively visit unvisited adjacent vertices */

       

}

   

}

}

/* BFS: breadth-first search, implemented with a queue
   characteristics: expand layer by layer; naturally suited for finding the shortest path */

void

BFS

(

int

start

)

{

    bool visited

[

V

]

=

{

false

}

;

   

int

queue

[

V

]

;

   

int

front

=

0

,

rear

=

0

;

    visited

[

start

]

=

true

;

    queue

[

rear

++

]

=

start

;

 

/* enqueue the starting vertex */

   

while

(

front

<

rear

)

{

int

v

=

queue

[

front

++

]

;

 

/* dequeue */

       

printf

(

"%d "

,

v

)

;

       

for

(

int

i

=

0

;

i

<

V

;

i

++

)

{

           

if

(

graph

[

v

]

[

i

]

&&

!

visited

[

i

]

)

{

                visited

[

i

]

=

true

;

                queue

[

rear

++

]

=

i

;

 

/* enqueue adjacent vertices */

           

}

       

}

   

}

}

int

main

(

)

{

   

/* initialize the visited flags */

   

for

(

int

i

=

0

;

i

<

V

;

i

++

)

visited

[

i

]

=

false

;

   

printf

(

"DFS (starting from vertex 0): "

)

;

    DFS

(

0

)

;

 

/* output: 0 1 3 5 4 2 */

   

printf

(

"\n"

)

;

   

printf

(

"BFS (starting from vertex 0): "

)

;

    BFS

(

0

)

;

 

/* output: 0 1 2 3 4 5 */

   

printf

(

"\n"

)

;

   

return

0

;

}


Graph application scenarios

Scenario Typical algorithms
Shortest path Dijkstra, Bellman-Ford (map navigation)
Minimum spanning tree Prim, Kruskal (network cabling optimization)
Topological sort Kahn's algorithm (task dependency analysis)
Social network analysis PageRank, community detection