Lecture 13: Intro to Graphs
CSE 373: Data Structures and Algorithms
CSE 373 21 SP – CHAMPION
1
Administrivia
The Trie: A Specialized Data Structure - Runtime
3
CSE332, Spring 2021
L02: Dictionary ADT, Tries
a
m
d
p
e
w
l
s
Trie
s
a
Set ADT
Introduction to Graphs
CSE 373 SP 18 - KASEY CHAMPION
4
Inter-data Relationships
CSE 373 SP 18 - KASEY CHAMPION
5
A
B
C
0 | 1 | 2 |
A | B | C |
A
B
C
Inter-data Relationships
0 | 1 | 2 |
A | B | C |
Arrays
Trees
B
A
C
B
A
C
Graphs
Graphs
Applications
CSE 373 SP 18 - KASEY CHAMPION
8
SO MANY MORREEEE
Graph: Formal Definition
CSE 373 SP 18 - KASEY CHAMPION
9
A
B
C
D
E
F
G
H
V = { A, B, C, D, E, F, G, H }
E = { (A, B), (A, C), (A, D), (A, H),
(C, B), (B, D), (D, E), (D, F),
(F, G), (G, H)}
Graph Vocabulary
CSE 373 SP 20 - KASEY CHAMPION
10
Karen
Jim
Pam
V = { Karen, Jim, Pam }
E = { (Jim, Pam), (Jim, Karen) } inferred (Karen, Jim) and (Pam, Jim)
V = { Gunther, Rachel, Ross }
E = { (Gunther, Rachel), (Rachel, Ross), (Ross, Rachel) }
Gunther
Rachel
Ross
Undirected Graph:
Directed Graph:
Karen : 1, Jim : 2, Pam : 1
Gunther : 0, Rachel : 2, Ross : 1
Gunther : 1, Rachel : 1, Ross : 1
More More Graph Terminology
a
b
c
f
e
g
d
j
p
m
n
i
o
p
m
n
i
o
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:
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
Some examples
CSE 373 SU 19 – ROBBIE WEBBER
Multi-Variable Analysis
Adjacency Matrix
CSE 373 SU 19 – ROBBIE WEBBER
| 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)
Adjacency List
CSE 373 SP 20 - KASEY CHAMPION
17
A
B
C
D
Linked Lists
0 | | |
1 | | |
2 | | |
3 | | |
A
B
C
D
A
B
C
B
D
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)
𝚯(deg(u) )
𝚯(deg (u) )
𝚯(deg(u) )
𝚯(n + m)
𝚯(n + m)
Adjacency List
CSE 373 SP 20 - KASEY CHAMPION
18
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)
Tradeoffs
373: Graph Implementations
Questions / clarifications on anything?
relevant ideas for today