Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

1

Lecture 1 - Graph Models

AMA3820 Operations Research Methods

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

Undirected graph Directed graph

Remark: The name “network” is often used to refer to a graph, especially in


computer science and social science. We use both names in this course
interchangeably.
AMA3820 Operations Research Methods Lecture 1 - Graph Models 2 / 48
Graph

Why do we use graphs?

Good for visualization of a problem.


Representing a problem as a graph can simplify the problem.
It provides tools for analyzing the problem.

Graph theory (Network theory)

A set of techniques for analyzing graphs.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 3 / 48


Real-life examples/applications of graphs

Transportation networks

(Source: www.oocl.com)

AMA3820 Operations Research Methods Lecture 1 - Graph Models 4 / 48


Real-life examples/applications of graphs

Social networks
(Source: www.facebook.com)

AMA3820 Operations Research Methods Lecture 1 - Graph Models 5 / 48


Real-life examples/applications of graphs

Functional brain network


(Source: https://integratedlistening.com/)

AMA3820 Operations Research Methods Lecture 1 - Graph Models 6 / 48


Definitions
A graph is an ordered pair G = (N, E ) comprising
▶ a set N of nodes and;
▶ a set E of edge, and each edge is a 2-element subset of N.
Two types: directed graphs (also called digraphs) and undirected graphs.
For undirected graph, the set E consists of undirected edges of the form
{i, j}, where i, j are two nodes in N.
▶ {i, j} = {j, i}.

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)

AMA3820 Operations Research Methods Lecture 1 - Graph Models 7 / 48


Example 1.1

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}}

AMA3820 Operations Research Methods Lecture 1 - Graph Models 8 / 48


Definitions

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

AMA3820 Operations Research Methods Lecture 1 - Graph Models 9 / 48


Definitions

A weighted graph is a graph for which each edge has an associated


weight (or length or cost).

4 1
1 2
3 5 3 4
3 1 4 2
2 3 4
3 4

Weighted graphs

AMA3820 Operations Research Methods Lecture 1 - Graph Models 10 / 48


Definitions
A path (or undirected path) in an undirected graph is a sequence of
distinct edges which connect a sequence of distinct nodes.
1
3 4
4 2
2 3 4

Path: ({1, 2}, {1, 3}, {3, 4})

A directed path (or simply called a path) in a directed graph is defined


similarly, except that the edges must be traversed in the direction of the
arrows, so that the tip of one arrow must join the tail of the next arrow.
4
1 2
3 5
3 1
3 4

Directed path: ((1, 2), (2, 4)) and ((1, 3), (3, 4))

AMA3820 Operations Research Methods Lecture 1 - Graph Models 11 / 48


Further examples of graph models in real applications

In Electrical Engineering, graph theory is used in designing of circuit


connections. These circuit connections are named as topologies. Some
topologies are series, bridge, star and parallel topologies.
In linguistics, graphs are mostly used for parsing of a language tree.
Graph theory is also used in sociology. For example, to explore rumor
spreading, or to measure actors’ prestige notably through the use of social
network analysis software.
Graph models also exist in biology. Nodes in biological networks represent
bimolecular such as genes, proteins or metabolites, and edges connecting
these nodes indicate functional, physical or chemical interactions between
the corresponding bimolecular.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 12 / 48


Further examples of graph models in real applications

Graphs can be used to represent networks of communication.


Graphs can be used to represent data organization. Graph transformation
systems work on rule-based in-memory manipulation of graphs. Graph
databases ensure transaction-safe, persistent storing and querying of
graph structured data.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 13 / 48


Definitions

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

Cycle in a connected graph A disconnected graph

AMA3820 Operations Research Methods Lecture 1 - Graph Models 14 / 48


Shortest Path Problem

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

AMA3820 Operations Research Methods Lecture 1 - Graph Models 15 / 48


Shortest Path Problem

In the previous example the problem was to determine a path in a graph that
described travel between towns.

If we do not wish to rely on exhaustive enumeration (how many possible paths


are there between towns A and H?) which for reasonable sized problems would
take unreasonably long, we must use a more judicious approach.

The following method is due to Dijkstra (pronounced like “dai-k-stre”) and


applies to graphs with non-negative edge lengths.

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.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 16 / 48


Dijkstra’s algorithm — a bit of history
What is the shortest way to travel from Rotterdam to Groningen, in general: from
given city to given city. It is the algorithm for the shortest path, which I designed
in about twenty minutes.

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.

As I said, it was a twenty-minute invention. In fact, it was published in ’59, three


years later. The publication is still readable, it is, in fact, quite nice. One of the
reasons that it is so nice was that I designed it without pencil and paper. I learned
later that one of the advantages of designing without pencil and paper is that you
are almost forced to avoid all avoidable complexities.

Eventually, that algorithm became to my great amazement, one of the cornerstones


of my fame.

— Edsger Dijkstra, in an interview with Philip L. Frana, Communications of


the ACM, 2001

AMA3820 Operations Research Methods Lecture 1 - Graph Models 17 / 48


Dijkstra’s Algorithm
The input of the Dijkstra’s algorithm is a weighted graph G = (N, E )
with weights denoted by aij for each edge (i, j) ∈ E (or {i, j} ∈ E in an
undirected graph), a starting node and a destination node. We let
N = {1, . . . , n}, 1 be the starting node and n be the destination node.

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.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 18 / 48


Dijkstra’s Algorithm
Iterates of the algorithm:
▶ c: current node;
▶ U: the set of unvisited nodes;
▶ V ; the set of visited nodes;
▶ di : tentative distance from node 1 to node i;
▶ bi : best neighbor of node i (i.e., the neighbor of node i that
leads to the tentative distance di ).
The optimal path can be found by tracing the best neighbors.
The starting node is also called the source node.
The destination node is also called the target node.
If a node i is labelled as visited, its tentative distance di will not be
changed and equals to the true distance from node 1 to node i.
If you want to find the shortest path from a certain node to each other
node, then you should terminate the algorithm when U = ∅.
Upon termination, the shortest path can be obtained by tracking the best
neighbors backward.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 19 / 48


Example 1.2
Example 1.2 Determine the shortest path from node 1 to node 8.

22
2 5

25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32

3 6
42

Initialization: V = ∅, U = {1, . . . , 8}, d1 = 0, d2 = · · · = d8 = ∞.


Iteration 1: c = 1, V = {1}, U = {2, . . . , 8}.
d2 = 25, b2 = 1
d3 = 22, b3 = 1

AMA3820 Operations Research Methods Lecture 1 - Graph Models 20 / 48


Example 1.2
Example 1.2 Determine the shortest path from node 1 to node 8.

22
2 5

25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32

3 6
42

Iteration 2: c = 3, V = {1, 3}, U = {2, 4, 5, 6, 7, 8}.


d2 = min{25, 22 + 31} = 25, b2 = 1
d4 = 22 + 16 = 38, b4 = 3
d6 = 22 + 42 = 64, b6 = 3

AMA3820 Operations Research Methods Lecture 1 - Graph Models 21 / 48


Example 1.2
Example 1.2 Determine the shortest path from node 1 to node 8.

22
2 5

25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32

3 6
42

Iteration 3: c = 2, V = {1, 2, 3}, U = {4, 5, 6, 7, 8}.


d4 = min{38, 25 + 17} = 38, b4 = 3
d5 = 25 + 22 = 47, b5 = 2
d6 = 64, b6 = 3

AMA3820 Operations Research Methods Lecture 1 - Graph Models 22 / 48


Example 1.2
Example 1.2 Determine the shortest path from node 1 to node 8.

22
2 5

25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32

3 6
42

Iteration 4: c = 4, V = {1, 2, 3, 4}, U = {5, 6, 7, 8}.


d5 = min{47, 38 + 18} = 47, b5 = 2
d6 = 64, b6 = 3
d7 = 38 + 30 = 68, b7 = 4

AMA3820 Operations Research Methods Lecture 1 - Graph Models 23 / 48


Example 1.2
Example 1.2 Determine the shortest path from node 1 to node 8.

22
2 5

25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32

3 6
42

Iteration 5: c = 5, V = {1, 2, 3, 4, 5}, U = {6, 7, 8}.


d6 = 64, b6 = 3
d7 = min{68, 47 + 19} = 66, b7 = 5
d8 = 47 + 45 = 92, b8 = 5

AMA3820 Operations Research Methods Lecture 1 - Graph Models 24 / 48


Example 1.2

Example 1.2 Determine the shortest path from node 1 to node 8.

22
2 5

25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32

3 6
42

Iteration 6: c = 6, V = {1, 2, 3, 4, 5, 6}, U = {7, 8}.


d7 = min{66, 64 + 15} = 66, b7 = 5
d8 = min{92, 64 + 32} = 92, b8 = 5

AMA3820 Operations Research Methods Lecture 1 - Graph Models 25 / 48


Example 1.2

Example 1.2 Determine the shortest path from node 1 to node 8.

22
2 5

25 17 18 19 45
22
1 31 4 7 8
30
22 16 15 32

3 6
42

Iteration 7: c = 7, V = {1, 2, 3, 4, 5, 6, 7}, U = {8}.


d8 = min{92, 66 + 22} = 88, b8 = 7

Iteration 8: c = 8 (Terminated)

AMA3820 Operations Research Methods Lecture 1 - Graph Models 26 / 48


Example 1.2
Example 1.2 Determine the shortest path from node 1 to node 8.

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.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 27 / 48


Complexity Analysis of Dijkstra’s Algorithm

Consider a graph with n nodes.


(Step 2: dc = mini∈U di )
The first time Step 2 is reached, up to n − 1 comparisons are required;
the k-th time it is reached n − k comparisons are needed. Therefore,
total comparisons

(n − 1) + (n − 2) + · · · + 1 = O(n2 ).

(Step 3: di ← min{di , dc + aci })


n − 1 comparisons and n − 1 additions.

Computational Complexity O(n2 )

AMA3820 Operations Research Methods Lecture 1 - Graph Models 28 / 48


Example 1.3

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

AMA3820 Operations Research Methods Lecture 1 - Graph Models 29 / 48


Example 1.3

(2‚1)
4
2 4
2 2
2
1 1 3 6
4 2
3
3 5
(4‚1)

Initialization: V = ∅, U = {1, . . . , 6}, d1 = 0, d2 = · · · = d6 = ∞.


Iteration 1: c = 1, V = {1}, U = {2, . . . , 6}.
d2 = 2, b2 = 1
d3 = 4, b3 = 1

AMA3820 Operations Research Methods Lecture 1 - Graph Models 30 / 48


Example 1.3

(2‚1) (6‚2)
4
2 4
2 2
2
1 1 3 6
4 2
3
3 5
(3‚2) (4‚2)

Iteration 2: c = 2, V = {1, 2}, U = {3, 4, 5, 6}.


d3 = min{4, 2 + 1} = 3, b3 = 2
d4 = 2 + 4 = 6, b4 = 2
d5 = 2 + 2 = 4, b5 = 2

AMA3820 Operations Research Methods Lecture 1 - Graph Models 31 / 48


Example 1.3

(2‚1) (6‚2)
4
2 4
2 2
2
1 1 3 6
4 2
3
3 5
(3‚2) (4‚2)

Iteration 3: c = 3, V = {1, 2, 3}, U = {4, 5, 6}.


d5 = min{4, 3 + 3} = 4, b5 = 2

AMA3820 Operations Research Methods Lecture 1 - Graph Models 32 / 48


Example 1.3

(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)

Iteration 4: c = 5, V = {1, 2, 3, 5}, U = {4, 6}.


d4 = min{6, 4 + 3} = 6, b4 = 2
d6 = 4 + 2 = 6, b6 = 5

AMA3820 Operations Research Methods Lecture 1 - Graph Models 33 / 48


Example 1.3

(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)

Iteration 5: c = 6, V = {1, 2, 3, 5, 6}, U = {4} (Terminated).

b6 = 5 ⇒ b5 = 2 ⇒ b2 = 1

The shortest path is


1 → 2 → 5 → 6,
with length d6 = 6.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 34 / 48


Minimum Spanning Trees (MST)

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.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 35 / 48


Definition 1.4
1 3 5

2 4

Graph G
1 3 1 3

2 2 4

Trees of G.
1 3 5

2 4

A spanning tree of G.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 36 / 48


Example 1.5
Example 1.5 The telephone company wants to improve their telephone service
for an area consisting of buildings A-G shown in the graph below by installing a
microwave receiver at a certain building and some landlines to join the
buildings and share the received signals. Due to geographical features of the
area, not all buildings can be joined by a landline. For any two buildings that
can be joined, an edge is present in the graph with a weight representing the
cost of installing that particular landline.

7 5
A C E
5
7 7
10 11 G
5 6
8 9
B D F

Question: Determine the configuration that minimizes the installation cost.


(Since the buildings will all be joined by landlines at the end, it does not matter
where to put the receiver.)
Answer: The minimum spanning tree problem!

AMA3820 Operations Research Methods Lecture 1 - Graph Models 37 / 48


Prim’s Algorithm (aka, Jarník’s algorithm)

The input of the Prim’s algorithm is a weighted, undirected graph


G = (N, E ) with weights denoted by aij for each edge {i, j} ∈ E . We let
N = {1, . . . , n}.

Prim’s Algorithm

1 Initialization: Set V = {1} and F = ∅.


2 Add minimum edge: Find the minimum edge {i, j} from V to N \ V :

find an edge {i, j} s.t. aij = min akℓ ,


k∈V , ℓ∈N\V

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.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 38 / 48


Prim’s Algorithm

Iterates of the algorithm:


▶ V : the set of nodes in the minimum spanning tree.
▶ F : the set of edges in the minimum spanning tree.
Instead of node 1, on can also choose an arbitrary node as the initial
node of the tree in the initialization (Step 1).
When there is more than one edge attaining the minimum in Step (2),
you may pick an arbitrary one to proceed. Any choice from the minimum
edges would yield a correct MST at the end.
The minimum spanning tree can be recovered using the edges in F .

AMA3820 Operations Research Methods Lecture 1 - Graph Models 39 / 48


Example 1.6

2 3
5
9
1 4 6
8

1
5 3 10
6
5
7 3

P edge P edge length


1 2, 3, 4, 5, 6

AMA3820 Operations Research Methods Lecture 1 - Graph Models 40 / 48


Example 1.6

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}}

AMA3820 Operations Research Methods Lecture 1 - Graph Models 41 / 48


Example 1.6

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}}

AMA3820 Operations Research Methods Lecture 1 - Graph Models 42 / 48


Example 1.6

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}}

AMA3820 Operations Research Methods Lecture 1 - Graph Models 43 / 48


Example 1.6

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}}

AMA3820 Operations Research Methods Lecture 1 - Graph Models 44 / 48


Example 1.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

A minimum spanning tree.

The length of the MST is 1 + 3 + 4 + 3 + 5 = 16.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 46 / 48


Greedy Algorithms

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.

AMA3820 Operations Research Methods Lecture 1 - Graph Models 47 / 48


Example 1.5

7 5
A C E
5
7 7
10 11 G
5 6
8 9
B D F

Minimum spanning tree:

V min. edge edge length


A,B A–B 5
A,B,C A–C 7
A,B,C,E C–E 5
A,B,C,E,G E–G 5
A,B,C,E,F,G G–F 6
A,B,C,D,E,F,G C–D 7
35

AMA3820 Operations Research Methods Lecture 1 - Graph Models 48 / 48

You might also like