TNK104 Applied Optimization
Graph Algorithms, Part 1
Lecture 2
based on the slides provided by Nikolaos Pappas
Basic Terminology
2
TNK104 Applied Optimization I
Undirected graphs:
Example
Degree = 3
Graph G = (V, E)
Basic Terminology (cont’d)
3
TNK104 Applied Optimization I
Basic Terminology (cont’d)
4
TNK104 Applied Optimization I
Basic Terminology (cont’d)
5
TNK104 Applied Optimization I
Basic Terminology (cont’d)
6
TNK104 Applied Optimization I
Basic Terminology (cont’d)
7
TNK104 Applied Optimization I
Basic Terminology (cont’d)
8
TNK104 Applied Optimization I
Basic Terminology (cont’d)
9
Tree
Forest
Basic Terminology (cont’d)
10
TNK104 Applied Optimization I
Directed graphs:
Weighted graphs (now let’s put some values on edges/arcs):
Graphs: applications
11
12
Implementing a Graph
13
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
Implementing a Graph
14
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
Graph Representation
15
Graph Representation
16
Edge List: Pros and Cons
Graph Representation
17
Graph Representation
18
Graph Representation
19
Adjacency Matrix: Pros and Cons
Graph Representation
20
21
TNK104 Applied Optimization I
Graph Representation Example
Adjacency matrix + Adjacency lists
Traversal
22
TNK104 Applied Optimization I
Depth-first search: visit all vertices
prefer to go “deeper” if possible in visiting the vertices
See YT video in the Self-Study Material
What is the time complexity of the algorithm?
Traversal
23
TNK104 Applied Optimization I
Depth-first search: visit all vertices
prefer to go “deeper” if possible in visiting the vertices
See YT video in the Self-Study Material
What is the time complexity of the algorithm? O(V)
Breadth-first search: See Self-Study Material
24
TNK104 Applied Optimization I
Topological Sort
Given a directed acyclic graph G = (V, E), visit and print the vertices in an order such that each vertex v is printed before any vertex with (v, u) ∈ E
Observation: At least one vertex has in degree = 0 in a directed acyclic graph
A possible solution is v4 , v8 , v3 , v2 , v1 , v5 , v7 , v6
Can you think of some application?
Topological Sort (cont’d)
25
TNK104 Applied Optimization I
Time complexity?
Topological Sort (cont’d)
26
TNK104 Applied Optimization I
Time complexity?
Linear O(V+E)
Shortest Path
27
TNK104 Applied Optimization I
Shortest Path Tree
28
TNK104 Applied Optimization I
Consider the problem of finding shortest paths from v5 to all other vertices for the example graph
By the way: Is the solution always a tree?
Solution:
Solution Representation of the Shortest Path Tree
29
TNK104 Applied Optimization I
Principles of Dijkstra’s Algorithm
Initially: Let the shortest path tree T = (V 0, E0) be ({s}, ∅), where s denotes the source
30
TNK104 Applied Optimization I
Dijkstra’s Algorithm
31
TNK104 Applied Optimization I
Let’s “decode” by executing it …
Time complexity?
Dijkstra’s Algorithm
32
TNK104 Applied Optimization I
Greedy algorithm (detailed in lect 3)
Efficient: complexity O(V^2) (depends on implementation of the priority queue)
But! Does not work with negative weights (see examples in the self-study material)
Alternative: Bellman-Ford (dynamic programming approach)
robust, but less efficient: O(EV)
Shortest Path Variants
33
TNK104 Applied Optimization I
application: critical path (longest path)
> BFS works well
Self-Study Material
Textbook: Section 22 Elementary Graph Algorithms,
Section 24 Single-Source Shortest Paths (*25 All-Pairs SP)
MIT OCW video lectures:
# 13 https://youtu.be/s-CYnVz-uh4
# 14 https://youtu.be/AfSk24UTFS8
# 17 https://youtu.be/xhG2DyCX3uA
Cycle detection https://youtu.be/tg96sZqhXyU
Youtube videos:
Algorithms https://youtu.be/6hfOvs8pY1k , Graph applications https://youtu.be/_AFqdsqIs-A
Complexity https://youtu.be/zUUkiEllHG0, https://youtu.be/u2DLlNQiPB4
BFS https://youtu.be/xlVX7dXLS64 DFS https://youtu.be/PMMc4VsIacU, https://youtu.be/7fujbpJ0LB4
TopSort https://youtu.be/eL-KzMXSXXI, https://youtu.be/cIBFEhD77b4
Dijkstra’ https://youtu.be/EFg3u_E6eHU, https://youtu.be/pSqmAO-m7Lk
Bellman Ford’ https://youtu.be/lyw4FaxrwHg