Lecture12-The-Foundations_Graphs
Lecture12-The-Foundations_Graphs
and Algorithms
Lecture 12 – The Foundations ~ Graphs
[email protected]
Journey so far
• Sorting
• UFDS
• Ordered Map
2
What do you remember thus far? ☺
• Go to: https://menti.com (code: 29 66 92 5)
3
Road Ahead
4
Outline of this Lecture
A. Motivation on why you should learn graph
• Graph terminologies
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)
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
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
• 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
13
Transportation Network
Simple cycle
Sengkang LRT, west loop)
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
https://visualgo.net/en/graphds
18
Adjacency Matrix
• A 2D array (AdjMatrix)
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
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
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];
26
Counting Vertices
27
Counting Vertices
• Trivial for both AdjMatrix and AdjList: V ➔ number of rows!
28
Enumerating Neighbours
O(V) O(k)
29
Enumerating Neighbours
• O(V) for AdjMatrix: scan AdjMatrix[v][j], j [0..V-1]
31
Counting Edges
• O(1) for EdgeList
• Undirected/Bidirected edges may be listed once (or twice) in EdgeList,
depending on the need
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
35
Feedback
https://forms.office.com/r/jcLS2bxjth
36