Graph Representation: B.Tech (CSE), Semester 5, Analysis and Design of Algorithms
Graph Representation: B.Tech (CSE), Semester 5, Analysis and Design of Algorithms
Representation
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 }
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
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
4 4 3
Neighbor query
O(n) O(deg(v))
Give me v’s neighbors.
n is no. of vertices.
Exercise:
2
Draw adjacency matrix for the graph, G (V, E).
1
3
G = (V,E)
4
Graph Representation
Exercise:
[ ]
3
1
0 0 1 1 G = (V,E)
2
4
1 1 0 1
3
0 1 1 0
4
Graph Representation
Exercise:
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:
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
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.
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. 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.
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