Graph Theory
Graph Theory
Algorithm:
• Enqueue a starting node. Mark it as visited.
• While queue is NOT empty:
• Dequeue a node. Print it out.
• Enqueue ALL of ITS unvisited ADJACENT nodes.
Mark them as visited.
Algorithm:
• Push a starting node. Mark it as visited.
• While stack is NOT empty:
• Pop the stack. Print it out.
• Push ALL of ITS unvisited ADJACENT nodes.
Mark them as visited.
Each node/vertex is either visited or unvisited Each node/vertex is either visited or unvisited
Algorithm: Algorithm:
• Enqueue a starting node. Mark it as visited. • Push a starting node. Mark it as visited.
• While queue is NOT empty: • While stack is NOT empty:
• Dequeue a node. Print it out. • Pop the stack. Print it out.
• Enqueue ALL of ITS unvisited ADJACENT nodes. • Push ALL of ITS unvisited ADJACENT nodes. Mark
Mark them as visited. them as visited.
Central office
Central office
Expensive!
Central office
• Prim's algorithm:
• is agreedy algorithm
• finds a minimum spanning tree (MST) for
a weighted undirected graph
MST = {}
Unvisited = { A, B, C, D, E}
MST = {B}
Unvisited = { A, C, D, E}
MST = {B, D}
Unvisited = { A, C, E}
MST = {B, D, E}
Unvisited = { A, C}
MST = {B, D, E, C}
Unvisited = { A}
MST = {B, D, E, C, A}
Unvisited = {}
E F 2
F D 2
B C 3
C F 3
C D 4
B F 5
B D 6
A B 7
A C 8
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
GV: T.S. Nguyễn Quốc Minh 55
GV: T.S. Nguyễn Quốc Minh 56
GV: T.S. Nguyễn Quốc Minh 57
GV: T.S. Nguyễn Quốc Minh 58
GV: T.S. Nguyễn Quốc Minh 59
GV: T.S. Nguyễn Quốc Minh 60
GV: T.S. Nguyễn Quốc Minh 61
GV: T.S. Nguyễn Quốc Minh 62
Note: Since the number of edges included in the MST
equals to (V – 1), so the algorithm stops here
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342
77 GV: T.S. Nguyễn Quốc Minh
Prim or Kruskal
• Prim's algorithm is significantly faster in the limit when you've got a
really dense graph with many more edges than vertices.
• Kruskal performs better in typical situations (sparse graphs)
because it uses simpler data structures.
• We should use Kruskal when the graph is sparse, [Link] number of
edges,like E=O(V),when the edges are already sorted or if we can sort
them in linear time. We should use Prim when the graph is dense, i.e
number of edges is high ,like E=O(V²).
SPT = {}
• The heuristic function h(n) needs to be admissible; it should never overestimate the optimal
cost of reaching the destination node from the current node. If the heuristic is not
admissible, A* is not guaranteed to find the lowest-cost path.
• Manhattan Distance