Lecture 1
Lecture 1
Lecture 1
1
Thank Dr. M. C. Yue and Dr. T. K. Pong for providing many course
materials.
AMA3820 Operations Research Methods Lecture 1 - Graph Models 1 / 48
Graph
What is a graph?
A graph contains two types of objects: nodes (or vertices) and edges (or
arcs or links) which join the nodes.
The nodes are usually represented by circles.
The edges are represented by lines (in an undirected graph) or arrows (in
a directed graph).
1 2 3
1 2
6
4 5 3 4
Transportation networks
(Source: www.oocl.com)
Social networks
(Source: www.facebook.com)
1 2
Undirected edge: {1, 2}
For directed graph, the set E consists of directed edges of the form (i, j),
where i, j are two nodes in N.
▶ (i, j) ̸= (j, i) if i ̸= j.
1 2
Directed edge: (1, 2)
Example 1.1 The notation for describing the following graph is G = (N, E ),
where N is the set of nodes, and E is the set of edges.
1 3 5
2 4
N = {1, 2, 3, 4, 5}
E = {{1, 3}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}}
An edge of the form {i, i} (in undirected graph) or (i, i) (in directed
graph) is called a self-loop.
A graph is simple if it has no multiple edges or self-loops.
1 2 3
Non-simple graph
1 2 3
Simple graph
4 1
1 2
3 5 3 4
3 1 4 2
2 3 4
3 4
Weighted graphs
Directed path: ((1, 2), (2, 4)) and ((1, 3), (3, 4))
A cycle (or undirected cycle) is a path where the starting node and end
node are the same. Directed cycles are defined in the same manner.
Two nodes are connected if there exists an (undirected) path between
them. A graph is connected if every pair of nodes is connected.
2 4
1 2 3
1 6
6
3 5 4 5
Example 1.2 You wish to drive from town A to town H. Looking at the road
map below you notice that there are quite a few possible combinations of
routes, passing through other towns, B, C, D, E, F and G. The travel times on
the roads linking these towns is printed on the map. Choose the route that
takes you to your destination in shortest time.
22
B E
25 17 18 19 45
22
A 31 D G H
30
22 16 15 32
C F
42
In the previous example the problem was to determine a path in a graph that
described travel between towns.
Other algorithms that work for graphs where the edges can be take any values
may be found in “Combinatorial optimization, graphs and matroids” by E.
Lawler.
One morning I was shopping in Amsterdam with my young fiancée, and tired, we
sat down on the café terrace to drink a cup of coffee and I was just thinking about
whether I could do this, and I then designed the algorithm for the shortest path.
Dijkstra’s Algorithm
1 Initialize the tentative distances and visited nodes: Set d1 = 0,
di = ∞ for all i ∈ N \ {1}; and V = ∅, U = N = {1, . . . , n}.
2 Update current node: Set c as the unvisited node with minimum
tentative distance (i.e., dc = mini∈U di ). If c = n, then terminate.
3 Label current node as visited: Set V ← V ∪ {c}, U ← U \ {c}.
4 Update tentative distances and best neighbors: For all unvisited nodes
i ∈ U with (c, i) ∈ E (i.e., there is an edge from c to i), if dc + aci < di ,
then set di ← dc + aci and bi ← c; otherwise di and bi are unchanged
(i.e., di ← min{di , dc + aci }).
5 Go to Step 2.
Upon termination, the shortest path can be obtained by tracking the best
neighbors backward.
22
2 5
25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32
3 6
42
22
2 5
25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32
3 6
42
22
2 5
25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32
3 6
42
22
2 5
25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32
3 6
42
22
2 5
25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32
3 6
42
22
2 5
25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32
3 6
42
22
2 5
25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32
3 6
42
Iteration 8: c = 8 (Terminated)
22
2 5
25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32
3 6
42
b8 = 7 ⇒ b7 = 5 ⇒ b5 = 2 ⇒ b2 = 1
The shortest path is ({1, 2}, {2, 5}, {5, 7}, {7, 8}). More intuitively, you may
also write the path as
1 → 2 → 5 → 7 → 8,
with length d8 = 88.
(n − 1) + (n − 2) + · · · + 1 = O(n2 ).
Example 1.3 Find the shortest path from the source node 1 to each other node.
As an exam, we will only look for the shortest path to node 6. Do the others by
yourself.
4
2 4
2 2
2
1 1 3 6
4 2
3
3 5
(2‚1)
4
2 4
2 2
2
1 1 3 6
4 2
3
3 5
(4‚1)
(2‚1) (6‚2)
4
2 4
2 2
2
1 1 3 6
4 2
3
3 5
(3‚2) (4‚2)
(2‚1) (6‚2)
4
2 4
2 2
2
1 1 3 6
4 2
3
3 5
(3‚2) (4‚2)
(2‚1) (6‚2)
4
2 4
2 2
2
1 1 3 6
4 2 (6‚5)
3
3 5
(3‚2) (4‚2)
(2‚1) (6‚2)
4
2 4
2 2
2
1 1 3 6
4 2 (6‚5)
3
3 5
(3‚2) (4‚2)
b6 = 5 ⇒ b5 = 2 ⇒ b2 = 1
Definition 1.4
1 Given a graph G, a subgraph of G is graph formed by using nodes and
edges of G.
2 For a given graph G, a tree is a connected, acyclic (has no cycles)
subgraph of G.
3 If a tree contains every node of G then it is called a spanning tree.
4 A minimum spanning tree (MST) would be one with the lowest total
weights among all spanning trees.
2 4
Graph G
1 3 1 3
2 2 4
Trees of G.
1 3 5
2 4
A spanning tree of G.
7 5
A C E
5
7 7
10 11 G
5 6
8 9
B D F
Prim’s Algorithm
F ← F ∪ {{i, j}}.
3 Add new node: V ← V ∪ {j}. If V contains all the node (i.e., V = N),
then terminate.
4 Go to Step 2.
2 3
5
9
1 4 6
8
1
5 3 10
6
5
7 3
2 3
5
9
1 4 6
8
1
5 3 10
6
5
7 3
Initialization: V = {1}, F = ∅.
Iteration 1:
min. edge V F
{1, 2} {1, 2} {{1, 2}}
2 3
5
9
1 4 6
8
1
5 3 10
6
5
7 3
Iteration 2:
min. edge V F
{2, 5} {1, 2, 5} {{1, 2}, {2, 5}}
2 3
5
1 4 6
8
1
5 3 10
6
5
7 3
Iteration 3:
min. edge V F
{2, 4} {1, 2, 4, 5} {{1, 2}, {2, 5}, {2, 4}}
2 3
5
1 4 6
1
5 3 10
6
5
3
Iteration 4:
min. edge V F
{4, 6} {1, 2, 4, 5, 6} {{1, 2}, {2, 5}, {2, 4}, {4, 6}}
2 3
5
1 4 6
1
5 3 10
6
5
3
Iteration 5:
min. edge V F
{3, 4} {1, 2, 4, 5, 6, 3} {{1, 2}, {2, 5}, {2, 4}, {4, 6}, {3, 4}}
Terminate.
AMA3820 Operations Research Methods Lecture 1 - Graph Models 45 / 48
Example 1.6
2 3
5
1 4
1
3
6
5
3
The philosophy of this algorithm is very simple. At each iteration it will add
the closest node to the set P.
Algorithms that at each iteration choose the next item to be the one that is the
best in some shortsighted criterion, as for example here, are called greedy
algorithms.
Usually these methods are suboptimal (not optimal), but Prim’s algorithm is
one situation where the greedy algorithm finds the optimal solution.
7 5
A C E
5
7 7
10 11 G
5 6
8 9
B D F