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

Lecture12-The-Foundations_Graphs

The lecture covers the foundations of graphs, including key terminologies and various graph data structures such as adjacency matrix, adjacency list, and edge list. It emphasizes the importance of graphs in data structures and algorithms, providing motivation for their study. Additionally, the lecture outlines applications and basic operations associated with these graph data structures.

Uploaded by

Ngoh JS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture12-The-Foundations_Graphs

The lecture covers the foundations of graphs, including key terminologies and various graph data structures such as adjacency matrix, adjacency list, and edge list. It emphasizes the importance of graphs in data structures and algorithms, providing motivation for their study. Additionally, the lecture outlines applications and basic operations associated with these graph data structures.

Uploaded by

Ngoh JS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

CS2040 – Data Structures

and Algorithms
Lecture 12 – The Foundations ~ Graphs
[email protected]
Journey so far
• Sorting

• Lists With Thanks to

• HashTable Prof Ket Fah Chong

• Binary Heap Prof Roger Zimmermann

• UFDS

• Ordered Map
2
What do you remember thus far? ☺
• Go to: https://menti.com (code: 29 66 92 5)

3
Road Ahead

• Graphs, graphs and more graphs ☺

• Lots of very cool algorithms ☺

4
Outline of this Lecture
A. Motivation on why you should learn graph
• Graph terminologies

B. Three Graph Data Structures


• Adjacency Matrix
• Adjacency List
• Edge List
• https://visualgo.net/en/graphds

C. Some Graph Data Structure Applications

D. This lecture is setup for the rest of the module on graph DSAs
5
Graph Terminologies (1)
• Extension from what you already know: (Binary) Tree
• Vertex, Edge, Direction (of Edge), Weight (of Edge)

• But in a general graph, there is no notion of


• Root
• Parent/Child
• Ancestor/Descendant

6
Graph (?)
15

6 50

7 23 77
4

7
Graph (?)
7 23

6 50

15
77
4

5
8
Note: definitions here might be
Graph is… a bit different from CS1231/S

• Graph is a set of N vertices where some [0 .. NC2] pairs of the vertices


are connected by edges (3 types – undirected, directed, bi-directed)
• We will ignore “multi graph” where there can be more than
one edge
5 (of any edge type) between a pair of vertices
Weighted Undirected Edge
Weighted Bi-directed Edge
7
0 1 2 (made of 2 directed edges
going in opposite directions)
Undirected Edge
Thus vertex 0 and vertex 3 2 A graph with a combination
are called “adjacent” or 3 4 5 of undirected+directed edge
they are neighbours of each 3 is called a mixed graph
other
Directed Edge
A vertex, usually labeled from 0 to N-1 9
Example

10
Graph Terminologies (2)
• Sparse/Dense
Sparse Graph
• Sparse = not so many edges
• Dense = many edges
• No guideline for “how many” In/out degree
of vertex 5 = 3

• Complete Graph
• Simple graph with N vertices Dense Graph
and NC2 edges
Complete Graph
7 vertices,
• In/Out Degree of a vertex 7C2 = 21 edges

• Number of in/out edges


from a vertex
11
Graph Terminologies (3)
• (Simple) Path
• Sequence of vertices connected by a sequence of undirected edges
• Simple = no repeated vertex
• A path with only 1 vertex and no edge is an empty path

• (Simple) Directed Path


• Same as (Simple) Path with the added restriction that the edges in the
path are directed and in the same direction

• Path Length/Cost
• In unweighted graph, usually number of edges in the path
• In weighted graph, usually sum of edge weight in the path
12
Graph Terminologies (4)
• (Simple) Cycle
• Path that starts and ends with the same vertex and with no repeated
vertices except start/end vertex and no repeated edges

• Involves 3 or more unique vertices

• (Simple) Directed Cycle


• Same as (Simple) Cycle with the added restriction that the edges in the
cycle are directed and in the same direction

• Involves 2 or more unique vertices

13
Transportation Network

Simple cycle
Sengkang LRT, west loop)

Simple path (from Clementi to


Outram Park MRT) with length
7 (in terms of number of hops)

14
Internet/Computer/Communication Networks

15
Graph Terminologies (5)
• Component 3 components in this graph
• A maximal group of vertices in an • Disconnected graph (since it has > 1
undirected graph that can visit component)
each other via some path • Vertices 1-2-3-4 are reachable from
vertex 0
• Connected graph
• Vertices 5, 6-7-8 are unreachable from
• Undirected graph with 1 component
vertex 0
• Reachable/Unreachable Vertex • {7-6-8 5} is a sub graph of this graph
• See example

• Sub Graph
• Subset of vertices (and their
connecting edges) of the
original graph
16
Graph Terminologies (6)
• Directed Acyclic Graph (DAG) Out degree of vertex 0 = 2
• Directed graph that has no cycle

• Tree (bottom left)


• Connected graph – one
In degree of vertex 2 = 2
unique path between any pair
of vertices

• Bipartite Graph (bottom right)


• Undirected graph where we can so that
partition the vertices into two sets so
that there are no edges between
members of the same set
17
Graph Data Structures

https://visualgo.net/en/graphds

18
Adjacency Matrix
• A 2D array (AdjMatrix)

• AdjMatrix[i][j] = 1, if there exist an edge i ➔ j in G, otherwise 0

• For weighted graph, AdjMatrix[i][j] contains the weight of edge i ➔


j, not just binary values {1, 0}

• Space Complexity (V = number of vertices in G)


• O(V2)

19
Example of Adjacency Matrix

3 4 5 Adjacency Matrix
0 1 2 3 4 5 6
0 0 1 1 0 0 0 0
1 1 0 1 1 0 0 0
2 1 1 0 0 1 0 0
1 2 6 3 0 1 0 0 1 0 0
4 0 0 1 1 0 1 0
5 0 0 0 0 1 0 1
0 6 0 0 0 0 0 1 0

20
Adjacency List
• An array of V lists (AdjList)
• One element for each vertex

• For each vertex i, AdjList[i] = list of i’s neighbours


• For weighted graph, stores pair (neighbour, weight)
• Can use same strategy for unweighted graph
• Connected to neighbour? Set weight to 1 (unit weight), 0 otherwise

• Space Complexity (E = number of edges in G)


• O(V + E) (E = O(V2))

21
Example of Adjacency List

3 4 5
Adjacency List
0: 1 2
1: 0 2 3
2: 0 1 4
1 2 6 3: 1 4
4: 2 3 5
5: 4 6
6: 5
0

22
Edge List
• An array of E edges (EdgeList)
• One element for each edge

• For each edge i, EdgeList[i] = integer triple {u, v, w(u, v)}

• For unweighted graph, the weight can be stored as 0 (or 1), or


simply store an (integer) pair
• Space Complexity: O(E)

23
Example of Edge List
7 9 Edge List
3 4 5
0: 0 1 1
1: 0 2 4
5 5 3
2: 1 2 4
3: 1 3 5
4
1 2 6 4: 2 4 5
5: 3 4 7
1 4 6: 4 5 9

0 7: 5 6 3

24
Java Implementation (1)
Adjacency Matrix: Simple built-in 2D array
int V = NUM_V; // NUM_V has been set before
int[][] AdjMatrix = new int[V][V];

Adjacency List: With Java Collections framework


ArrayList < ArrayList < IntegerPair > >
AdjList = new ArrayList < ArrayList < IntegerPair > >();
// IntegerPair is a simple integer pair class
// to store pair info, see the next slide

Edge List: Also, with Java Collections framework


ArrayList < IntegerTriple > EdgeList =
new ArrayList < IntegerTriple >();
// IntegerTriple is similar to IntegerPair

PS: This is one implementation, there are other ways ☺


25
Graph Data Structures

What can we do with them?

Some basic calculations for now, but more later ☺

26
Counting Vertices

Adjacency Matrix Edge List


Adjacency List
0 1 2 3 4 5 6 0: 0 1 1
0: 1 2
0 0 1 1 0 0 0 0 1: 0 2 4
1: 0 2 3
1 1 0 1 1 0 0 0 2: 1 2 4
2: 0 1 4
2 1 1 0 0 1 0 0 3: 1 3 5
3: 1 4
3 0 1 0 0 1 0 0 4: 2 4 5
4: 2 3 5
4 0 0 1 1 0 1 0 5: 3 4 7
5: 4 6
5 0 0 0 0 1 0 1 6: 4 5 9
6: 5
6 0 0 0 0 0 1 0 7: 5 6 3

27
Counting Vertices
• Trivial for both AdjMatrix and AdjList: V ➔ number of rows!

• Sometimes this number is stored in separate variable so that


we do not have to re-compute this every time, that is, O(1),
especially if the graph never changes after it is created

• To think about: How about EdgeList?

28
Enumerating Neighbours

Adjacency Matrix Edge List


Adjacency List
0 1 2 3 4 5 6 0: 0 1 1
0: 1 2
0 0 1 1 0 0 0 0 1: 0 2 4
1: 0 2 3
1 1 0 1 1 0 0 0 2: 1 2 4
2: 0 1 4
2 1 1 0 0 1 0 0 3: 1 3 5
3: 1 4
3 0 1 0 0 1 0 0 4: 2 4 5
4: 2 3 5
4 0 0 1 1 0 1 0 5: 3 4 7
5: 4 6
5 0 0 0 0 1 0 1 6: 4 5 9
6: 5
6 0 0 0 0 0 1 0 7: 5 6 3

O(V) O(k)

29
Enumerating Neighbours
• O(V) for AdjMatrix: scan AdjMatrix[v][j], j  [0..V-1]

• O(k) for AdjList: scan AdjList[v]


• k is the number of neighbours of vertex v (output-sensitive algorithm)

• This is an important difference between AdjMatrix versus AdjList


• It affects the performance of many graph algorithms. Remember this!

• Usually, the neighbours are listed in increasing vertex number

• Again, what about EdgeList?


30
Counting Edges

Adjacency Matrix Edge List


Adjacency List
0 1 2 3 4 5 6 0: 0 1 1
0: 1 2
0 0 1 1 0 0 0 0 1: 0 2 4
1: 0 2 3
1 1 0 1 1 0 0 0 2: 1 2 4
2: 0 1 4
2 1 1 0 0 1 0 0 3: 1 3 5
3: 1 4
3 0 1 0 0 1 0 0 4: 2 4 5
4: 2 3 5
4 0 0 1 1 0 1 0 5: 3 4 7
5: 4 6
5 0 0 0 0 1 0 1 6: 4 5 9
6: 5
6 0 0 0 0 0 1 0 7: 5 6 3

O(V2) O(V) O(1)

31
Counting Edges
• O(1) for EdgeList
• Undirected/Bidirected edges may be listed once (or twice) in EdgeList,
depending on the need

• O(V2) for AdjMatrix: count non-zero entries in AdjMatrix

• O(V) for AdjList: sum the length of all V lists

• Sometimes this number is stored in separate variable so that


we do not have to re-compute this every time, i.e. O(1),
especially if the graph never changes after it is created
32
Existence of Edges
• Given vertices u and v, does edge(u,v) exist?

• O(1) for AdjMatrix: see if AdjMatrix[u][v] is non zero

• O(k) for AdjList: see if AdjList[u] contains v

• How about EdgeList?

33
Trade-Off
Adjacency Matrix Adjacency List

• Pros • Pros
• Existence of edge i – j can be • O(k) to enumerate k neighbours
found in O(1) of a vertex
• Good for dense graph/ Floyd • Good for sparse graph/Dijkstra’s/
Warshall’s DFS/BFS, O(V+E) space
• Cons • Cons
• O(V) to enumerate neighbours of • O(k) to check the existence of
a vertex edge i – j
• O(V2) space • A small overhead in maintaining
the list (for sparse graph)

34
Live Quiz ☺
• On Zoom

• And a question asking your opinion

35
Feedback
https://forms.office.com/r/jcLS2bxjth

36

You might also like