0% found this document useful (0 votes)
48 views

Graph Representations: Adjacency Matrix Adjacency Lists Adjacency Multilists

The document discusses different representations of graphs including adjacency matrices, adjacency lists, and adjacency multilists. It provides examples and details on how to represent graphs and perform operations using each representation. Adjacency matrices store connectivity information in a 2D array while adjacency lists and multilists use linked data structures. Common graph operations like traversal, connected components, and spanning trees are also briefly mentioned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Graph Representations: Adjacency Matrix Adjacency Lists Adjacency Multilists

The document discusses different representations of graphs including adjacency matrices, adjacency lists, and adjacency multilists. It provides examples and details on how to represent graphs and perform operations using each representation. Adjacency matrices store connectivity information in a 2D array while adjacency lists and multilists use linked data structures. Common graph operations like traversal, connected components, and spanning trees are also briefly mentioned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Graph Representations

 Adjacency Matrix
 Adjacency Lists
 Adjacency Multilists

CHAPTER 6 1
Adjacency Matrix
 Let G=(V,E) be a graph with n vertices.
 The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
 If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
 If there is no such edge in E(G), adj_mat[i][j]=0
 The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric

2
Examples for Adjacency Matrix
0 0 4
0
2 1 5
1 2
3 6
3 1
0 1 1 1 0 1 0
1 0 1 1    7
  1 0 1 
2 0 1 1 0 0 0 0 0
1 1 0 0 0 1 0
1 0
   0 0 1 0 0 0
1 1 1 0
1 0
G2 0 0 1 0 0 0
G1  
0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0
 
0 0 0 0 1 0 1 0
symmetric 0 0 0 0 0 1 0 1
 
undirected: n2/2 0 0 0 0 0 0 1 0
directed: n2
G4 3
Merits of Adjacency Matrix
 From the adjacency matrix, to determine the
connection of vertices is easy n 1

 The degree of a vertex is  adj _ mat[i][ j ]


j 0

 For a digraph, the row sum is the out_degree,


while the column sum is the in_degree
n 1 n 1
ind (vi )   A[ j , i ] outd (vi )   A[i , j ]
j 0 j 0

4
Data Structures for Adjacency Lists
Each row in adjacency matrix is represented as an adjacency list.

#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];
int n=0; /* vertices currently in use
*/
5
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
6
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Interesting Operations
degree of a vertex in an undirected graph
–# of nodes in adjacency list
# of edges in a graph
–determined in O(n+e)
out-degree of a vertex in a directed graph
–# of nodes in its adjacency list
in-degree of a vertex in a directed graph
–traverse the whole data structure

7
Compact Representation
0 4
node[0] … node[n-1]: starting point for vertices
2 1 5 node[n]: n+2e+1
3 6 node[n+1] … node[n+2e]: head node of edge

7
[0] 9 [8] 23 [16] 2
[1] 11 0 [9] 1 4 [17] 5
[2] 13 [10] 2 5 [18] 4
[3] 15 1 [11] 0 [19] 6
[4] 17 [12] 3 6 [20] 5
[5] 18 2 [13] 0 [21] 7
[6] 20 [14] 3 7 [22] 6
[7] 22 3 [15] 1
CHAPTER 6 8
Figure 6.10: Inverse adjacency list for G3

0
0  1 NULL

1 1  0 NULL
2  1 NULL
2

Determine in-degree of a vertex in a fast way.

CHAPTER 6 9
Figure 6.11: Alternate node structure for adjacency lists (p.267)

tail head column link for head row link for tail

CHAPTER 6 10
Figure 6.12: Orthogonal representation for graph G3(p.268)

0 1 2

0 0 1 NULL NULL

1 1 0 NULL 1 2 NULL NULL

0
2 NULL
0 1 0
 
 1 0 1 
0 0 0 1

2
CHAPTER 6 11
Figure 6.13:Alternate order adjacency list for G1 (p.268)
Order is of no significance.
headnodes vertax link

0  3  1  2 NULL

1  2  0  3 NULL

2  3  0  1 NULL

3  2  1  0 NULL

1 2
3 CHAPTER 6 12
Adjacency Multilists
An edge in an undirected graph is
represented by two nodes in adjacency list
representation.
Adjacency Multilists
–lists in which nodes may be shared among
several lists.
(an edge is shared by two different paths)

marked vertex1 vertex2 path1 path2

CHAPTER 6 13
Example for Adjacency Multlists
Lists: vertex 0: M1->M2->M3, vertex 1: M1->M4->M5
vertex 2: M2->M4->M6, vertex 3: M3->M5->M6

(1,0)
0 N1 0 1 N2 N4 edge (0,1)
1 (2,0)

2 N2 0 2 N3 N4 edge (0,2)
(3,0)
3 N3 0 3 N5 edge (0,3)
(2,1)

0 N4 1 2 N5 N6 edge (1,2)
(3,1)
N5 1 3 N6 edge (1,3)
1 2 (3,2)
N6 2 3 edge (2,3)
3
six edges
CHAPTER 6 14
Adjacency Multilists

typedef struct edge *edge_pointer;


typedef struct edge {
short int marked;
int vertex1, vertex2;
edge_pointer path1, path2;
};
edge_pointer graph[MAX_VERTICES];

marked vertex1 vertex2 path1 path2

CHAPTER 6 15
Some Graph Operations
 Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal
 Connected Components
 Spanning Trees
CHAPTER 6 16
*Figure 6.19:Graph G and its adjacency lists (p.274)
depth first search: v0, v1, v3, v7, v4, v5, v2, v6

breadth first search: v0, v1, v2, v3, v4, v5, v6, v7
CHAPTER 6 17
Depth First Search
#define FALSE 0
#define TRUE 1
short int visited[MAX_VERTICES];
void dfs(int v)
{
node_pointer w;
visited[v]= TRUE;
printf(“%5d”, v);
for (w=graph[v]; w; w=w->link)
if (!visited[w->vertex])
dfs(w->vertex); Data structure
} adjacency list: O(e)
adjacency matrix: O(n2)
CHAPTER 6 18
Breadth First Search
typedef struct queue *queue_pointer;
typedef struct queue {
int vertex;
queue_pointer link;
};
void addq(queue_pointer *,
queue_pointer *, int);
int deleteq(queue_pointer *);

CHAPTER 6 19
Breadth First Search (Continued)
void bfs(int v)
{
node_pointer w;
queue_pointer front, rear;
front = rear = NULL;
adjacency list: O(e)
printf(“%5d”, v); adjacency matrix: O(n2)

visited[v] = TRUE;
addq(&front, &rear, v);

CHAPTER 6 20

You might also like