Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Graph Data Structure

Terminology
Graph Directed, Undirected and mixed Graph Adjacent nodes and isolated node Incident Initating (originating), Terminating (ending) and loop node

Simple graph and multigraph


Weighted graph Null graph

Indegree, outdegree and total degree of vertice

Terminology

Terminology
Path
Elementary path (edge simple) Simple path (node simple)

Terminology
Cycle (circuit)
Elementary Cycle

Acyclic graph

Representation of Graph
Adjacency Matrix Adjacency List Using Linked List (i.e. Sparse Matrix method)

Graph Representation
Adjacency Matrix

Graph Representation
Adjacency list for graph
V1 V2 V3 V4 V2 V4 V1 V1 V2 V4 V2

1 2 3 4

V1 V2 V3 V4

V2 V1 V1

V4

V2

V4

V2

Breadth First Search


Breadth First Search (BFS) can be used to find the shortest

distance between some starting node and the remaining nodes of the graph Shortest distance is the minimum number of edges traversed in order to travel from the node to the specific node being examined Starting at node v, this distance is calculated by examining all incident edges to node v, and then moving on to an adjacent node w and repeating the process.

BFS Algorithm
Node Structure
REACH NODENO DATA DIST LISTPTR

Node table directory structure

DESTIN

EDGEPTR

Edge structure

BFS Algorithm
Procedure: BFS(INDEX) INDEX current node being processed LINK points to the edge being examined REACH denotes whether node is traversed or not QUEUE name of the queue QINSERT and QDELETE queue handling procedures

BFS Algorithm
1.

[Initialize the first nodes DIST number and place node queue]
REACH[INDEX] true DIST[INDEX] 0 Call QINSERT(QUEUE, INDEX)

2. 3. 4.

[Repeat until all nodes have been examined]


Repeat thru step 5 while queue is not empty

[Remove current node to be examined from queue]


Call QDELETE(QUEUE, INDEX)

[Find all unlabeled nodes adjacent to current node]


LINK LISTPTR[INDEX] Repeat step 5 while LINK != NULL

5.

[If this is an unvisited node, label it and add it to the queue]


If not REACH[DESTIN(LINK)] then DIST[DESTIN(LINK)] DIST[INDEX] + 1 REACH[DESTIN(LINK)] true Call QINSERT(QUEUE, DESTIN(LINK)) LINK EDGEPTR(LINK) (move down edge list)

6.

[Finished]
Return

Depth First Search


Depth First Search (DFS) can be used to perform a traversal

of a general graph.

DFS Algorithm
Procedure: DFS(INDEX, COUNT) INDEX current node being processed LINK points to the edge being examined REACH denotes whether node is traversed or not DFN depth first search number

DFS Algorithm
1.

[Update the depth first search number, set and mark current node]
COUNT COUNT + 1 DFN[INDEX] COUNT REACH[INDEX] true

2.

[Set up loop to examine each neighbor of current node]


LINK LISTPTR[INDEX] Repeat step 3 while LINK != NULL

3.

[If node has not been marked, label it and make recursive call]
If not REACH[DESTIN(LINK)] then Call DFS (DESTIN(LINK), COUNT) LINK EDGEPTR(LINK) (Examine next adjacent node)

4.

[Return to point of call]


Return

End of Chapter

You might also like