Lecture 15: Intro to Graphs
CSE 373: Data Structures and Algorithms
1
CSE 373 23SP
CSE 373 23SP
1
Announcements
Slido Event #2085756
CSE 373 23SP
2
Introduction to Graphs
Graph Modelling
CSE 373 23SP
3
Inter-data Relationships
0 | 1 | 2 |
A | B | C |
Arrays
Trees
B
A
C
B
A
C
Graphs
CSE 373 23SP
4
Graphs
CSE 373 23SP
5
Applications
Physical Maps
Relationships
Influence
Related topics
And so many more!!
CSE 373 23SP
6
Graph: Formal Definition
A graph is defined by a pair of sets G = (V, E) where…
(C, B), (B, D), (D, E), (D, F),
(F, G), (G, H)}
A
B
C
D
E
F
G
H
CSE 373 23SP
7
Graph Vocabulary
Graph Direction
Degree of a Vertex
Karen
Jim
Pam
Gunther
Rachel
Ross
Undirected Graph:
Directed Graph:
CSE 373 23SP
8
More More Graph Terminology
Two vertices are connected if there is a path between them
A path is a sequence of vertices connected by edges
a
b
c
f
e
g
d
j
p
m
n
i
o
p
m
n
i
o
not connected
connected
CSE 373 23SP
9
Directed vs Undirected; Acyclic vs Cyclic
a
b
d
c
a
b
d
c
e
a
b
d
c
a
b
d
c
Acyclic:
Cyclic:
Directed:
Undirected:
CSE 373 23SP
10
Labeled and Weighted Graphs
Vertex & Edge Labels
Edge Labels
a
b
c
d
Vertex Labels
b
d
c
e
a
Numeric Edge Labels�(Edge Weights)
1
2
3
1
2
3
4
5
1
a
b
c
d
CSE 373 23SP
11
Multi-Variable Analysis
CSE 373 23SP
12
Adjacency Matrix
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
2 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
3 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
4 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
5 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
6
2
3
4
5
0
1
In an adjacency matrix a[u][v] is 1 if there is an edge (u,v), and 0 otherwise.
Worst-case Time Complexity �(|V| = n, |E| = m):
Add Edge:
Remove Edge:
Check edge exists from (u,v):
Get outneighbors of u:
Get inneighbors of u:
Space Complexity:
𝚯(1)
𝚯(1)
𝚯(1)
𝚯(n)
𝚯(n)
𝚯(n * n)
CSE 373 23SP
13
Adjacency List
In an adjacency matrix a[u][v] is 1 if there is an edge (u,v), and 0 otherwise.
Worst-case Time Complexity �(|V| = n, |E| = m):
Add Edge:
Remove Edge:
Check edge exists from (u,v):
Get outneighbors of u:
Get inneighbors of u:
Space Complexity:
A
B
C
D
Linked Lists
0 | | |
1 | | |
2 | | |
3 | | |
A
B
C
D
A
B
C
B
D
𝚯(1)
𝚯(deg(u))
𝚯(deg (u))
𝚯(deg(u))
𝚯(n + m)
𝚯(n + m)
CSE 373 23SP
14
Adjacency List
0 | 1 | 2 | 3 | 4 |
| | | | |
0 | 1 | 2 | 3 | 4 |
| | | | |
0 | 1 | 2 | 3 | 4 |
| | | | |
A
B
C
D
Hash Tables
0 | | |
1 | | |
2 | | |
3 | | |
A
B
C
D
C
D
A
B
B
In an adjacency matrix a[u][v] is 1 if there is an edge (u,v), and 0 otherwise.
Worst-case Time Complexity �(|V| = n, |E| = m):
Add Edge:
Remove Edge:
Check edge exists from (u,v):
Get outneighbors of u:
Get inneighbors of u:
Space Complexity:
𝚯(1)
𝚯(1)
𝚯(1)
𝚯(deg(u) )
𝚯(n)
𝚯(n + m)
CSE 373 23SP
15
Tradeoffs
Adjacency Matrices take more space, why would you use them?
What’s the tradeoff between using linked lists and hash tables for the list of neighbors?
CSE 373 23SP
16
373: Graph Implementations
For this class, unless we say otherwise, we’ll assume the hash tables operations on graphs are all O(1)
Unless we say otherwise, assume we’re using the hash table approach
CSE 373 23SP
17
relevant ideas for today
Questions?
CSE 373 23SP
18
Introduction to Graphs
Graph Modelling
CSE 373 23SP
19
Some examples
Vertices: webpages. Edges from a to b if a has a hyperlink to b.
Directed, since hyperlinks go in one direction
Vertices: webpages. Edges from a to b if a has a hyperlink to b.
Directed, since hyperlinks go in one direction
Vertices: actors. Edges: movies
Undirected, a both actor would need to be in the movie for the edge to be added
Vertices: courses. Edge: from a to b if a is a prereq for b.
Directed, since one course comes before the other
Vertices: buildings. Edges: A street name or walkway that connects 2 buildings
Undirected, since each route can be walked both ways
CSE 373 23SP
20
Graph Modeling
SCENARIO
&
QUESTION TO ANSWER
ANSWER!
MODEL AS A GRAPH
RUN ALGORITHM
…
Often need to refine original model as you work through details of algorithm
Many ways to model any scenario with a graph, but question motivates which data is important
CSE 373 23SP
21
Graph Modeling Activity
Note Passing - Part I
Imagine you are an American High School student. You have a very important note to pass to your crush, but the two of you do not share a class so you need to rely on a chain of friends to pass the note along for you. A note can only be passed from one student to another when they share a class, meaning when two students have the same teacher during the same class period.
Unfortunately, the school administration is not as romantic as you, and passing notes is against the rules. If a teacher sees a note, they will take it and destroy it. Figure out if there is a sequence of handoffs to enable you to get your note to your crush.�
How could you model this situation as a graph?
| Period 1 | Period 2 | Period 3 | Period 4 |
You | Smith | Patel | Lee | Brown |
Anika | Smith | Lee | Martinez | Brown |
Bao | Brown | Patel | Martinez | Smith |
Carla | Martinez | Jones | Brown | Smith |
Dan | Lee | Lee | Brown | Patel |
Crush | Martinez | Brown | Smith | Patel |
�
students
teachers
CSE 373 23SP
22
Possible Design
You
Anika
Carla
Bao
Dan
Crush
Smith, 1
Martinez, 1
Patel, 2
Lee, 2
Martinez, 3
Brown, 3
Smith, 4
Patel, 4
You | | |
A | | |
B | | |
C | | |
D | | |
Crush | | |
A
B
B
D
You
A
C
You
D
Crush
B
C
Crush
A
C
D
CSE 373 23SP
23
More Design
Note Passing - Part II
Now that you know there exists a way to get your note to your crush, we can work on picking the best hand off path possible.
Thought Experiments:
2. What if you want to optimize for rick avoidance to make sure your note only gets passed in classes least likely for it to get intercepted?
- Some teachers are better at intercepting notes than others. The more notes a teacher has intercepted, the more likely it is they will take yours and it will never get to your crush. If we knew how many notes each teacher has intercepted how might we incorporate that into our graph to find the least risky route?
CSE 373 23SP
24
Optimize for Time
You
Anika
Carla
Bao
Dan
Crush
1
1
2
2
3
3
4
4
Vertex | Distance | Predecessor | Process Order |
You | 0 | -- | 0 |
Anika | 1 | You | 1 |
Bao | 2 | You | 5 |
Carla | 6 | Dan | 3 |
Dan | 3 | Anika | 2 |
Crush | 7 | Carla | 4* |
*The path found wraps around to a new school day because the path moves from a later period to an earlier one
- We can change our algorithm to check for wrap arounds and try other routes
“Distance” will represent the sum of which periods the note is passed in, because smaller period values are earlier in the day the smaller the sum the earlier the note gets there except in the case of a “wrap around”
CSE 373 23SP
25
Optimize for Risk
You
Anika
Carla
Bao
Dan
Crush
1
3
2
4
3
5
1
4
Vertex | Distance | Predecessor | Process Order |
You | 0 | -- | 0 |
Anika | 1 | You | 1 |
Bao | 4 | Anika | 2 |
Carla | 5 | Bao | 3 |
Dan | 10 | Carla | 5 |
Crush | 8 | Carla | 4 |
Teacher | Notes Intercepted |
Smith | 1 |
Martinez | 3 |
Lee | 4 |
Brown | 5 |
Patel | 2 |
“Distance” will represent the sum of notes intercepted across the teachers in your passing route. The smaller the sum of notes the “safer” the path.
CSE 373 23SP
26
Graph Modeling Activity
Party Planning
Imagine you are planning a party for your friend Steven. You are trying to figure out who you should invite from his big group of friends.
You want to invite as many people as possible, but you only want to invite either people he knows or friends of his friends, aka if they have at least one mutual friend.
You also don’t want to invite two people if they dislike each other.�
How could you model this situation as a graph?
Steven
Ali
Taylor
Lucy
Sasha
Bob
Johnny
Paul
Phoebe
Julie
Daisy
Joni
Margot
Melissa
Julien
Raven
Derrick
CSE 373 23SP
27
Graph Modeling Activity
Movie Theater Marathon
Imagine (for some reason) your goal for the day is to watch as many movies as possible at your local movie theater.
Your rules are that you have to be there for the start of the trailers (aka when the listed movie time is) and can’t leave a movie until it is completely finished (aka after the listed runtime).
How would you model this situation as a graph?
CSE 373 23SP
28
Questions?
CSE 373 23SP
29
That’s all!
CSE 373 23SP
30