CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡
Winter ¡2017 ¡
¡
Tim ¡Pierson ¡
260 ¡(255) ¡Sudikoff ¡
CS 10: Problem solving via Object Oriented Programming - - PowerPoint PPT Presentation
CS 10: Problem solving via Object Oriented Programming Winter 2017 Tim Pierson 260 (255) Sudikoff Day 15 RelaHonships Agenda 1. Graph
¡
260 ¡(255) ¡Sudikoff ¡
2 ¡
3 ¡
4 ¡
5 ¡
6 ¡
Image: ¡nbc.com ¡
Kevin ¡Bacon ¡
9 ¡
Images: ¡Facebook, ¡Twi]er, ¡1designshop.com ¡
10 ¡
city ¡or ¡computer ¡or ¡ intersecHon ¡of ¡roads… ¡
both ¡direcHons ¡
single ¡direcHons ¡
Only ¡undirected ¡edges ¡
Only ¡directed ¡edges ¡
Has ¡both ¡directed ¡and ¡ undirected ¡edges ¡
0 ¡ 1 ¡ 4 ¡ 3 ¡ 2 ¡
11 ¡
Person ¡object ¡
relaHonship ¡
(“follower”, ¡“friend”, ¡“co-‑ worker”) ¡
0 ¡ 1 ¡ 4 ¡ 3 ¡ 2 ¡
12 ¡
0 ¡ 1 ¡ 4 ¡ 3 ¡ 2 ¡
Count ¡of ¡edges ¡out ¡of ¡or ¡into ¡a ¡node ¡
Other ¡nodes ¡connected ¡from/to ¡a ¡node ¡
hasEdge
True ¡if ¡one ¡node ¡connected ¡to ¡another ¡
getLabel
Return ¡label ¡on ¡edge ¡from ¡one ¡node ¡to ¡ another ¡
insertVertex
Add ¡node ¡to ¡graph ¡
insertDirected/Undirected
Add ¡edge ¡to ¡graph ¡between ¡two ¡nodes ¡
removeVertex/Directed/Undirected
Remove ¡node ¡or ¡edge ¡
13 ¡
(most ¡in ¡edges) ¡
(“cliques” ¡where ¡all ¡nodes ¡have ¡ edges ¡to ¡each ¡other) ¡
is ¡not ¡yet ¡a ¡friend? ¡(breadth-‑ first ¡search, ¡next ¡class) ¡
13 ¡
14 ¡
15 ¡
16 ¡
0 ¡ 1 ¡ 4 ¡ 3 ¡ 2 ¡
{ {0,1}, {0,4}, {1,2}, {1,3}, {1,4}, {2,3}, {3,4} }
¡ Notes: ¡
reference ¡nodes ¡in ¡array ¡
between ¡two ¡nodes ¡
remove ¡all ¡edges ¡to/from ¡node, ¡so ¡search ¡ all ¡edges ¡leading ¡to ¡or ¡from ¡node ¡
17 ¡
¡ ¡ ¡ ¡ Notes: ¡
each ¡vertex ¡(same ¡if ¡undirected) ¡
neighbors ¡and ¡one ¡for ¡out ¡neighbors ¡
List, ¡just ¡iterate ¡in ¡O(degree(v)) ¡vs. ¡ O(m) ¡
0 ¡ 1 ¡ 4 ¡ 3 ¡ 2 ¡
18 ¡
Notes: ¡
from ¡node ¡i ¡to ¡node ¡j, ¡else ¡0 ¡
Adjacency ¡List ¡it ¡was ¡O(degree(u)) ¡
to ¡check ¡enHre ¡row ¡or ¡column ¡
rebuild ¡enHre ¡matrix ¡
0 ¡ 1 ¡ 4 ¡ 3 ¡ 2 ¡
From ¡ To ¡
2 ¡
Notes: ¡
adjacent ¡nodes ¡
19 ¡
0 ¡ 1 ¡ 4 ¡ 3 ¡ 2 ¡
0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 1 ¡ 4 ¡ 2 ¡ 3 ¡ 4 ¡ 1 ¡ 3 ¡ 1 ¡ 4 ¡ 2 ¡ 0 ¡ 1 ¡ 3 ¡
20 ¡
Method ¡ Edge ¡ ¡ List ¡ Adjacency ¡ List ¡ Adjacency ¡ Matrix ¡ Adjacency ¡ Map ¡
in/
O(m) ¡ O(1) ¡ O(n) ¡ O(1) ¡
in/
O(dv) ¡ O(n) ¡ O(dv) ¡
hasEdge(u,v)
O(m) ¡ O(min(du,dv)) ¡ O(1) ¡ O(1) ¡
insertVertex(v) O(1) ¡
O(1) ¡ O(n2) ¡ O(1) ¡
removeVertex(v) O(m) ¡
O(dv) ¡ O(n2) ¡ O(dv) ¡
insertEdge(u,v) O(1) ¡
O(1) ¡ O(1) ¡ O(1) ¡
removeEdge(u,v) O(m) ¡
O(1) ¡ O(1) ¡ O(1) ¡
{{0,1}, {0,4}, {1,2}, {1,3}, {1,4}, {2,3}, {3,4}}
¡
n ¡= ¡number ¡of ¡nodes ¡(5), ¡m ¡= ¡number ¡of ¡edges ¡(7), ¡dv ¡= ¡degree ¡of ¡node ¡v ¡
2" 0" 1" 2" 3" 4" 1" 4" 2" 3" 4" 1" 3" 1" 4" 2" 0" 1" 3"
21 ¡
22 ¡
23 ¡
24 ¡
25 ¡
26 ¡
27 ¡
28 ¡
Image: ¡nbc.com ¡
Kevin ¡ Bacon ¡
Other ¡ Actor ¡ Other ¡ Actor ¡ Other ¡ Actor ¡ Other ¡ Actor ¡ Other ¡ Actor ¡ Other ¡ Actor ¡
(most ¡in ¡edges) ¡
(“cliques” ¡where ¡all ¡nodes ¡have ¡ edges ¡to ¡each ¡other) ¡
is ¡not ¡yet ¡a ¡friend? ¡(breadth-‑ first ¡search, ¡next ¡class) ¡
2 ¡
¡ ¡ ¡ Notes: ¡
from ¡node ¡i ¡to ¡node ¡j, ¡else ¡0 ¡
Adjacency ¡List ¡it ¡was ¡O(degree(u)) ¡
to ¡check ¡enHre ¡row ¡or ¡column ¡
rebuild ¡enHre ¡matrix ¡
29 ¡
0 ¡ 1 ¡ 4 ¡ 3 ¡ 2 ¡
a ¡ e ¡ b ¡ c ¡ d ¡ f ¡
0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 1 ¡ 4 ¡ a ¡ e ¡ 2 ¡ 3 ¡ b ¡ g ¡
g ¡
4 ¡ f ¡ 1 ¡ 3 ¡ b ¡ c ¡ 1 ¡ 4 ¡ g ¡ d ¡ 2 ¡ 0 ¡ 1 ¡ b ¡ g ¡ 3 ¡ f ¡
30 ¡
in/outDegree(v)
in/outNeighbors(v) O(m) ¡
hasEdge(u,v)
insertVertex(v)
removeVertex(v)
insertEdge(u,v)
removeEdge(u,v)
{{0,1}, {0,4}, {1,2}, {1,3}, {1,4}, {2,3}, {3,4}} ¡
n ¡= ¡number ¡of ¡nodes, ¡m ¡= ¡number ¡of ¡edges, ¡dv ¡= ¡degree ¡of ¡node ¡v ¡