Intro To Graphs Representations of A Graph
Intro To Graphs Representations of A Graph
Presented By
Muhammad Atif Tahir
From
Haidong Xue
Why graphs are important
• Graphs are used a lot in computer science
– Social network (face book, linked in)
Why graphs are important
• Graphs are used a lot in computer science
– Computer networks
Why graphs are important
• Graphs are used a lot in computer science
– Transportation network
Why graphs are important
• Graphs are used a lot in computer science
– Wireless sensors
What are graphs?
• A set of vertices V and a set of edges E
• Each edge is a ordered pair of two vertices
• G = (V, E)
• E.g.
– V = {1, 2, 3, 4, 5, 6}
– E = {<1, 2>, <1, 4>, <2, 5>, <3, 6>, <3, 5>, <4, 2>,
<5, 4>, <6, 6>}
What are graphs?
V = {1, 2, 3, 4, 5, 6}
E = {<1, 2>, <1, 4>, <2, 5>, <3, 6>, <3, 5>, <4, 2>,
<5, 4>, <6, 6>}
1 2 3
4 5 6
What are graphs?
• Undirected graph
– A special graph
– If 𝑣𝑖 ≠ 𝑣𝑗 < 𝑣𝑖 , 𝑣𝑗 >∈ E, < 𝑣𝑗 , 𝑣𝑖 >∈ E
– E.g.
V = {1, 2, 3, 4, 5, 6}
E = {<1, 2>, <1, 4>, <2, 5>, <3, 6>, <3, 5>, <4, 2>, <5, 4>, <6, 6>}
∪ {<2, 1>, <4, 1>, <5, 2>, <6, 3>, <5, 3>, <2, 4>, <4, 5>}
1 2 3
Use a undirected
line to indicate a
pair of edges or a
self edge
4 5 6
How to represent a graph?
• Adjacency-matrix
– A |𝑉| × |𝑉| matrix
– if < 𝑣𝑖 , 𝑣𝑗 >∈ E, the matrix element 𝑎𝑖𝑗 = 1
– if< 𝑣𝑖 , 𝑣𝑗 >∉ E, the matrix element 𝑎𝑖𝑗 = 0
• E.g. …
How to represent a graph?
1 2 3
• E.g.
4 5 6
1 2 3 4 5 6
1 0 1 0 1 0 0
2 0 0 0 0 1 0
3 0 0 0 0 1 1
4 0 1 0 0 0 0
5 0 0 0 1 0 0
6 0 0 0 0 0 1
1 2 3 4 5 6
Can we improve the
space complexity? 1 0 1 0 1 0 0
2 0 0 0 0 1 0
3 0 0 0 0 1 1
4 0 1 0 0 0 0
5 0 0 0 1 0 0
6 0 0 0 0 0 1
How to represent a graph?
• Adjacency-list
– A arc adjacency list(Adj[]) with a size of |V|
– After each element in the adjacency list, there is a
arc node list
– A arc node is:
• {Destination vertex;
• Next arc node}
– For each < 𝑣𝑖 , 𝑣𝑗 >∈ E, there is a arc node:
• In the arc node list of Adj[i]
• With the destination vertex j
How to represent a graph?
• Adjacency-list
– An adjacency list with size of |V|
– After each arc head, there is a arc listFor each <
𝑣𝑖 , 𝑣𝑗 >∈ E, there is a arc node.
2 next
1 2 next 4 next NIL
1 2 3
2 5 next NIL
5 next
4 next 6 next 3 5 next 6 next NIL
2 next
5 next 4 2 next NIL
5
4 5 6 4 next NIL
4 next 6 6 next NIL
6 next
Adjacency-list
How to represent a graph?
• How to represent an attribute for an edge or a
node?
• Put satellite in vertices or edges and store
them Edge Edge
1 2 next 4 next NIL
1 2 3 4 5 6
1 Vertex
1 NIL Edge NIL Edge NIL NIL
2 Vertex
2 NIL NIL NIL NIL Edge NIL
3 Vertex
3 NIL NIL NIL NIL Edge Sat.
4 Vertex
4 NIL Edge NIL NIL NIL NIL
5 Vertex
5 NIL NIL NIL Sat. NIL NIL
6 Vertex
6 NIL NIL NIL NIL NIL Edge