0% found this document useful (0 votes)
27 views33 pages

Graph Representation: B.Tech (CSE), Semester 5, Analysis and Design of Algorithms

The adjacency matrix for the given graph G(V,E) is: 1 3 G = (V,E) 4 [1 0 1 0] [0 0 1 1] [1 1 0 1] [0 1 1 0]

Uploaded by

Prajjwal Mehra
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)
27 views33 pages

Graph Representation: B.Tech (CSE), Semester 5, Analysis and Design of Algorithms

The adjacency matrix for the given graph G(V,E) is: 1 3 G = (V,E) 4 [1 0 1 0] [0 0 1 1] [1 1 0 1] [0 1 1 0]

Uploaded by

Prajjwal Mehra
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/ 33

Graph

Representation

B.Tech (CSE), Semester 5,


Analysis and Design of Algorithms
Objectives
This session will enable students to:

1. Represent graph using different data structures.

2. Make appropriate choice of graph storage technique.

3. Define graph terminology.


Outcomes
After completing this session, you will be able to:

1. Assess the graph storage techniques.

2. Define graph terminology.


Graphs

• A graph is a set of vertices and a collection of edges that each


connect a pair of vertices. Graphs are the representations of
structures, set relations and states and state-transitions.
Undirected Graphs
Notation. G = (V, E)

・ V = nodes (or vertices).


・ E = edges (or arcs) between pairs of nodes.
・ Captures pairwise relationship between objects.

V = { 1, 2, 3, 4, 5, 6, 7, 8 }

E = { 1–2, 1–3, 2–3, 2–4, 2–5, 3–5, 3–7, 3–8, 4–5, 5–6, 7–8 }

|V| = 11, |E| = 8


Directed Graphs
Notation. G = (V, E).
・ Edge (u, v) leaves node u and enters node v.

1 2

7 6 3

5 4

Ex. Web graph: hyperlink points from one web page to another.
・ Orientation of edges is crucial.
・ Modern web search engines exploit hyperlink structure to rank web pages by
importance.
Graph Representation

Option 1: Adjacency matrix.


• n-by-n matrix with Auv = 1 if (u, v) is an edge.
• Two representations of each edge.
• Space proportional to n2.
• Checking if (u, v) is an edge takes O (1) time.

1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 0 1 0 0 0

5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
Graph Representation
Option 1: Adjacency matrix.
• n-by-n matrix with Auv = 1 if (u, v) is an edge.
• Two representations of each edge.
• Space proportional to n2.
• Checking if (u, v) is an edge takes O (1) time.

1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 0 1 0 0 0

5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
Graph Representation
Option 2: Adjacency list.
• node-indexed array of lists.
• Two representations of each edge.
degree = number of neighbors of u
• Space is O(m + n).
• Checking if (u, v) is an edge takes O(degree(u)) time.

1 3 2

2 1 3 4 5

3 2 1 5 7 8

4 2 5

5 2 3 4 6

6 5

7 3 8

8 3 7
Graph Representation
Option 2: Adjacency list.
• node-indexed array of lists.
• Two representations of each edge.
degree = number of neighbors of u
• Space is O(m + n).
• Checking if (u, v) is an edge takes O(degree(u)) time.
1 3 2

2 1 3 4 5

3 2 1 5 7 8

4 2 5

5 2 3 4 6

6 5

7 3 8

8 3 7
Graph Representation

• Adjacency matrix and adjacency list are valid to both directed and undirected graphs.
• Adjacency list is a compact way to represent sparse graphs (graphs for which |E| is less
than |v|2).
• Adjacency matrix is used when graph is dense (graphs for which |E| is close to |v|2).

1 3 2

1 2 3 4 5 6 7 8 2 1 3 4 5

1 0 1 1 0 0 0 0 0 3 2 1 5 7 8
2 1 0 1 1 1 0 0 0 4 2 5
3 1 1 0 0 1 0 1 1
4 0 1 0 0 1 0 0 0 5 2 3 4 6

5 0 1 1 1 0 1 0 0 6 5
6 0 0 0 0 1 0 0 0 7 3 8
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0 8 3 7

G (V, E) Adjacency Matrix Adjacency List


Trade Offs
 1 0 1 0 1 2 3 4

Say there are n


vertices and m edges. [ 0
1
0
0
1
1
1
0
1
1
1
0
] 3 3 1 2

4 4 3

Edge membership O(deg(v)) or


Is e = {v,w} in E?
O(1)
O(deg(w))

Neighbor query
O(n) O(deg(v))
Give me v’s neighbors.
n is no. of vertices.

Space O(n2) O(n + m)


requirements n is no. of vertices.
m is no. of edges.
Graph Representation

Exercise:
2
Draw adjacency matrix for the graph, G (V, E).
1
3
G = (V,E)
4
Graph Representation
Exercise:

Draw adjacency matrix for the graph, G (V, E).


2
1 2 3 41
 
0 0 1 0

[ ]
3
1

0 0 1 1 G = (V,E)
2

4
1 1 0 1
3

0 1 1 0
4
Graph Representation
Exercise:

Draw adjacency matrix for the graph, G (V, E).


2
1
3
G = (V,E)
4
Graph Representation
Exercise:

Draw adjacency matrix for the graph, G (V, E).


2

1 2 3 4 1
 
1 0 1 0 3
1

[ 0 0 1 1
] G = (V,E)
2

4
1 1 0 1
3

0 1 1 0
4
Graph Representation
Exercise:

2
Draw adjacency matrix for the graph, G (V, E).

1
3

4 G = (V,E)
Graph Representation
Exercise:

2
Draw adjacency matrix for the graph, G (V, E).

Destination
1 2 3 4
1
 
1

0 0 1 0 3

[ 0 0 0 1
]
2
Source

4 G = (V,E)

0 1 0 1
3

0 0 1 0
4
Graph Representation
Exercise:

Draw adjacency list for the graph, G (V, E).


2
1
3
G = (V,E)
4
Graph Representation
Exercise:

Draw adjacency list for the graph, G (V, E).


2
1 2 3 4
1
3
G = (V,E)
3 3 1 2 4

4 2 3
How would you modify
4’s neighbors are 2
4 and 3
this for directed
graphs?
Graph Representation
Exercise:

2
Draw adjacency list for the graph, G (V, E).

1
3

4 G = (V,E)
Graph Representation
Exercise:

2
Draw adjacency list for the graph, G (V, E).

1 2 3 4
1
3

3 4 4 3 4 G = (V,E)

3
Graph Representation

• If graph G is undirected, sum of lengths of all adjacency lists is 2|E|.


For an edge (u, v), u appears in v’s adjacency list and vice-a-versa.
• If graph G is directed, sum of lengths of all adjacency lists is |E|.
For an edge (u, v), v appears in u’s adjacency list only.

1 2 3 4 1 2 3 4

3 3 1 2 3 4 4 3

2 2 3
4 3
1
4
3

4
Graph Representation: Space Analysis

Exercise:
Suppose we have an undirected graph with 10 million vertices. Each vertex has at most 20
neighbors. Calculate the minimum space needed to store this graph in each representation.

Use/assume:
• A matrix of BITS for the matrix representation.
• An int is stored on 8 bytes and a memory address is stored on 8 bytes as well.
• Calculate the space requirement for each representation.
Graph Representation: Space Analysis

Exercise:
Suppose we have an undirected graph with 10 million vertices. Each vertex has at most 20
neighbors. Calculate the minimum space needed to store this graph in each representation.

Use/assume:
• A matrix of BITS for the matrix representation.
• An int is stored on 8 bytes and a memory address is stored on 8 bytes as well.
• Calculate the space requirement for each representation.

Adjacency matrices: we need at least 100 trillion bits of memory, so at least 12.5TB of memory.
Adjacency lists: in total, they would store at most 200 million nodes. With 16 bytes per node (as an
example), this takes at most 3.28 Gigabytes.
Graph Representation: Space Analysis
Steps:
• Suppose we have an undirected graph with:
 10 million vertices.
 Each vertex has at most 20 neighbors.
• Adjacency matrices: we need at least 100 trillion bits of memory, so at least 12.5TB of
memory.
• Adjacency lists: in total, they would store at most 100 million nodes. With 16 bytes per
node (as an example), this takes 1.68 Gigabytes.

• Find ‘keywords’, understand numbers:


 10 million vertices => 10 * 106
 Trillion = 1012
 1 TB (terra bytes) = 1012 bytes
 1GB = 109 bytes
 100 Trillion bits vs 12.5 TB
Graph Representation: Space Analysis
Steps (Adjacency Matrix):
• Suppose we have a graph with:
 10 million vertices. => |V| = 10*106 =107
 Each vertex has at most 20 neighbors.

• Adjacency matrix representation for the graph:


 The smallest possible matrix: a 2D array of bits =>
 The matrix size will be: |V| x |V| x 1bit =>
 107 * 107 * 1bit = 1014 bits
 Bits => bytes:
 1byte = 8 bits => 1014 bits = 1014/8 bytes = 100/8*1012 bytes = 12.5*1012 bytes
 12.5*1012 bytes = 12.5 TB (final result)
1012 bytes = 1TB
Graph Representation: Space Analysis
Steps (Adjacency List):
• Suppose we have a graph with:
 10 million vertices. => |V| = 10*106 =107
 Each vertex has at most 20 neighbors.

• Adjacency list representation for the graph:


 For each vertex, keep a list of edges (a list of neighboring vertices)
 Space for the adjacency list array:

= 10 million vertices*8 bytes (memory address) = 8*10 7 bytes = 0.08 GB

 Space for all the nodes (from the list for each vertex):
≤ 107vertices * (20 neighbors/vertex) = 20*107 nodes = 2*108 nodes

Assume 16 bytes per node: 8 bytes for the next pointer, and 8 bytes for the data (vertex):
2*108 nodes * 16byte/node = 32 * 108 bytes = 3.2 * 109 bytes = 3.2GB
Total: 3.2GB + 0.08 GB = 3.28GB ( 109 bytes = 1GB (GigaByte) )
Graph Terminology:
Paths and Connectivity
Def. A path in an undirected graph G = (V, E) is a sequence of nodes v1, v2, …, vk with the
property that each consecutive pair vi–1, vi is joined by a different edge in E.

Def. A path is simple if all nodes are distinct.

Def. An undirected graph is connected if for every pair of nodes u and v, there is a path
between u and v.
Graph Terminology: Cycles
Def. A cycle is a path v1, v2, …, vk in which v1 = vk and k ≥ 2.

Def. A cycle is simple if all nodes are distinct (except for v1 and vk ).

Cycle C = 1 - 2 - 4 - 5 - 3 - 1
Graph Terminology: Trees
Def. An undirected graph is a tree if it is connected and does not contain a cycle.

Theorem. Let G be an undirected graph on n nodes. Any two of the following


statements imply the third:
・ G is connected.
・ G does not contain a cycle.
・ G has n – 1 edges.
Graph Terminology: Rooted Trees
Given a tree T, choose a root node r and orient each edge away from r.

Importance. Models hierarchical structure.

root r

the parent of v

a child of v

a tree t he s a m e tree, ro o t e d at 1
Summary

• Undirected and Directed Graphs


• Graph Representation- Adjacency Matrix
• Graph Representation- Adjacency List
• Tradeoffs- Adjacency Matrix vs Adjacency List
• Graph Terminology

You might also like