Uploaded by Abdulhaseeb Khan

Lec20-CS202-Graphs-I

advertisement
LECTURE-20
Graphs
What are graphs? How can we represent them in computer systems?
CS202: Data Structures
Agenda
•
Graph Basics
−
•
Properties & Types
Graph Data Structures
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Trees and Hierarchical Relationships
•
Trees represent hierarchical
relationship
−
•
B
But not every relationship is
hierarchical
This is not a tree
−
Contains cycle (more than one
way to get from A to B
CS202, Spring 2023
A
Dr. Maryam Abdul Ghafoor
LUMS
Inter-data Relationships
Arrays
• Elements only store
pure data, no
connection info
• Only relationship
between data is
order
CS202, Spring 2023
Trees
• Elements store data
and connection info
• Directional
relationships between
nodes; limited
connections
Dr. Maryam Abdul Ghafoor
Graphs
• Elements and connections
can store data
• Relationship dictate
structure; huge freedom
with connections
LUMS
Graphs
•
Everything is in graphs
Internet
• Google maps database
• Facebook
• Gitlab history of repository
• Family Tree
•
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Graphs…
•
Physical maps
− Airline maps
•
Vertices? Edges?
− Traffic maps
•
Relationships
− Social media graphs
− Code bases
•
Influence
− Biology
•
Related topics
− Web page ranking
− wikipedia
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Graphs
•
Graphs are one of the most important abstractions in CS
−
•
They offer a way to express relations (e.g., events, people, cells)
Graphs are more expressive than trees
−
A tree cannot have cycles and multiple paths between any two nodes
are not allowed
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
What is a Graph?
•
A graph G is a pair of sets, V and E → G=(V,E)
− V: set of vertices (or nodes)
− E: set of edges (pairs of vertices)
− |V|: size of V
− |E|: size of E
b
b
d
d
c
c
a
h
g
V={a,b,c,d,e,f,g}
E={(c,d),(d,b),(d,a),
(c,e),(e,h)}
e
e
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Graph Vocabulary
b
b
•
Graph Direction
−
•
−
•
a
d
d
c
undirected graphs
f
e
Edges have no direction and are two way
• E = {(e, c), (c, e), (d, b)…..}
a
c
h
g
e
V = {a, b, c, d, e, h}
Directed graphs (diagraphs)
b
Edges have direction and are one way
• E = {(a, b), (a, c), (c, f), (c, e), (c, d), (g, d)}
a
d
c
f
g
e
V = {a, b, c, d, e, f, g}
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Graph Vocabulary
•
b
Degree of a Vertex
−
b
a
d
d
Degree is the number of edges incident
on a vertex
c
f
deg(e) = 2
g
a
c
h
e
e
− In-degree is the number of edges directed towards a
vertex, deg(e) = 1
b
a
− Out-degree is the number of edges directed aways
from the vertex, deg(a) = 2
d
c
f
g
e
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Graphs with Self-edges
Self-edge: An edge of the form (v,v)
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Example: Finding the Degree of a Vertex
•
Deg(2)? In-degree? Outdegree?
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Paths
•
Path: A path from vertex a to b is a sequence of vertices that can be followed
starting from a to reach b
− Can be represented as edges taken
− A simple path repeats no vertices, except the first might also be the last
− Example, one path from 1 to 4: {1, 2, 3, 4}
•
Path length: Number of edges in the path
−
•
Distance is the length of the shortest path
Neighbor or adjacent: Two vertices
connected directly by an edge
−
Ex: 1 and 3
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
W
g
Reachability, Connectedness
Y
f
•
Connected: A graph is connected if every
a
b
vertex is reachable from
every
other
− Called strongly connected
in
case
of
digraphs
c
d
•
a
b
d
c
a
e
a
Reachable: Vertex Y is reachable from V
if a path exists from Y to V
V
b
d
U
c
a
CS202, Spring 2023
c
g
f
b
c
d
Y
a
b
b
d
Dr. Maryam Abdul Ghafoor
h
e
Complete: If every vertex has a direct
edge to every other
a
c
X
W
•
U
d
c
e
LUMS
Z
Cycles
•
A cycle is a path whose first and last vertices are the same
− Ex: {V, X, Y, W, U, V} and {U, W, V, U}
− Cyclic graph is a graph with a cycle
− A simple cycle is a cycle with no repeated vertices (except the starting
vertex)
• Ex-1: {V, X, W, V, X, W, V} is NOT a simple cycle
− Note: For undirected graphs, we require that edges in a cycle be
distinct
− Why? The path {V, X, V} should NOT be considered a cycle because {V,
X} and {X, V} are the same edge whereas in a digraph, they are different
so it makes sense to call them a cycle in case of latter
•
a
V
b
d
U
c
X
h
e
W
g
f
Y
Acyclic graph: One that does not contain any cycles
CS202, Spring 2023
Dr. Maryam Abdul Ghafoor
LUMS
Z
Download