100% found this document useful (1 vote)
46 views14 pages

Intro To Graphs Representations of A Graph

This document discusses graphs and their representations in computer science. It defines what a graph is as a set of vertices and edges, and provides an example graph. It then explains two common ways to represent graphs: adjacency matrices and adjacency lists. Adjacency matrices store connectivity information in a 2D array, but use more space, while adjacency lists store edges as linked lists from each vertex to reduce space usage when graphs are sparse. The document also discusses how attributes of vertices and edges can be stored.
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
100% found this document useful (1 vote)
46 views14 pages

Intro To Graphs Representations of A Graph

This document discusses graphs and their representations in computer science. It defines what a graph is as a set of vertices and edges, and provides an example graph. It then explains two common ways to represent graphs: adjacency matrices and adjacency lists. Adjacency matrices store connectivity information in a 2D array, but use more space, while adjacency lists store edges as linked lists from each vertex to reduce space usage when graphs are sparse. The document also discusses how attributes of vertices and edges can be stored.
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/ 14

Design and Analysis of Algorithms

Introduction 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

What is the space complexity? S(n) = Θ(|𝑉|2 )


How to represent a graph?
• Adjacency-matrix
– Advantage
• Simple
• For some operations, it is efficient, e.g.: isConnected(< 𝑣𝑖 , 𝑣𝑗 >)
– Disadvantage
• When |E| is small, Θ(|𝑉|2 ) is a waste of space

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

You might also like