Lecture 10 - Graph
Lecture 10 - Graph
Objectives
At the end of this topic, you should be able
to:
Analyze
and define memory requirements for
GRAPH ADT
Applyprogramming skills to implement GRAPH
data structures.
Recommended Readings
February 2, 2025
+ What is a Graph 4
Airline
routes
Flowcharts
Network
Topologies February 2, 2025
+ 5
What is a Graph
A set of points (Nodes/Vertices) that are connected by lines (Edges)
Mathematically,
G = { V , E };
February 2, 2025
+ 6
Example
849 PVD
1843 ORD 2
SFO 14
802
337
743 LGA
2555
1
387 10
HNL 1 99
1233
LAX 1120
DFW
MIA
February 2, 2025
+ 7
Graph Terminology
A graph is connected if there is a path from each vertex to at
least one other vertex
February 2, 2025
+ 8
Graph Terminology
Subgraph: Subset of the graph’s vertices and the edges
connecting those vertices
February 2, 2025
+ 9
Graph Terminology
Path: Sequence of edges that allows one vertex to be reached from
another vertex
A vertex is reachable from another vertex if and only if there is a path between
the two vertices
Simple path: Path that does not pass through the same vertex more than
once BCDB
February 2, 2025
+ 10
Types of Graph
February 2, 2025
+ 11
Undirected graph
A graph in which edges have no direction.
Or
E = {{6, 4}, {4, 3}, {5, 4}, {5, 2}, {5, 1}, {3, 2}, {2, 1}}
February 2, 2025
+ 12
A graph in which vertices are connected by edges and edges have a direction
associated with them.
Example
V = {1, 2, 3, 4, 5, 6}
E = {(1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (5, 4), (4, 6)}
Can’t use the reverse paths, because they are not directed in reverse.
February 2, 2025
E = {(6, 4), (4, 3), (4, 5), (5, 2), (5, 1), (3, 2), (2, 1)}
+ 13
Weighted Graph
A weighted graph associates a label (weight) with every edge in the
graph.
Degree of a Vertex
Ref:
https://www.tutorialspoint.com/graph_theory/graph_theory_fundamentals.htm
https://algs4.cs.princeton.edu/42digraph/
February 2, 2025
+ 15
Degree of a Vertex
February 2, 2025
+ 16
February 2, 2025
+ 17
Adjacency Matrix
Using Matrix to represent graph in the memory
If a graph has N vertices labeled 0, 1, . . . , N – 1:
The adjacency matrix for the graph is a grid G with N rows and N
columns
Cell G[i][ j] = 1 if there’s an edge from vertex i to j
February 2, 2025
+ 18
1 2 3 1 2 3 4
1 0 1 1 0
1 0 0 1
2 1 0 0 0
2 0 1 0 3 1 0 0 0
3 1 1 0 4 0 0 0 0
February 2, 2025
+ 19
0 0 1 0
1 1 0 1
2 0 0 0
0 1 2 3
0 0 1 1 1
1 1 0 1 1
Size of the matrix: 16
2 1 1 0 1 Number of edges: 6
3 1 1 1 0
February 2, 2025
+ 20
February 2, 2025
+ 21
February 2, 2025
+ 22
1 3 1 2 3
2 1
2 2
3 1
3 12
4
February 2, 2025
+ 23
Example
February 2, 2025
+ 24
February 2, 2025
+ 25
February 2, 2025
+ 26
Ref:
https://www.youtube.com/watch?v=0u78hx-66Xk
https://www.youtube.com/watch?v=Y40bRyPQQr0
https://www.tutorialspoint.com/data_structures_algorithms/depth_first_traversal.htm
https://algorithms.tutorialhorizon.com/breadth-first-search-in-disconnected-graph/
February 2, 2025
https://algorithms.tutorialhorizon.com/graph-depth-first-search-using-recursion/
+ 27
Application of Graphs
Graphs serve as models for a variety of applications:
A roadmap
A map of airline routes
A layout of an adventure game world
A schematic of the computers and connections that make up the Internet
The links between pages on the Web
The relationship between students and courses
A diagram of the flow capacities in a communications or transportation
network
February 2, 2025
+ 28
1. Java Implementation
(DiGraph using Arrays)
February 2, 2025
+ 29
1. Java Implementation
(DiGraph using Arrays)
February 2, 2025
+ 30
1. Java Implementation
(DiGraph using Arrays)
February 2, 2025
+ 31
Extra
2. Java Implementation
(Weighted Undirected
Graph LL)
>> LL class
February 2, 2025
+ 32
Extra
(Weighted Undirected
Graph LL)
February 2, 2025
+ 33
Extra
(Weighted Undirected
Graph LL)
February 2, 2025
+ 34
Extra
February 2, 2025
+ 35
End of Graph- 11
February 2, 2025