You are on page 1of 71

Data Structures

SCS 214
2nd Term 2021-2022

Graphs

1
Agenda
1. Graph representation
2. Graph traversal
3. Shortest path
• Dijkstra
• Floyd
4. Minimum spanning tree
• Prim

2
Graph Definitions

• Trees are 1:n data structures that are suitable


for representing hierarchal data.
• What are their limitations?
• Graphs are generalizations of trees that are not
restricted to parent-child relations.
• Graphs can represent relations between any two
node directly.
• Graphs are n:n data structures.
Graph Definitions

• A graph is a set of nodes and edges .


• A simple graph G = (V, E) consists of a non-
empty set V, whose members are called the
vertices of G, and a set E of pairs of distinct
vertices from V, called the edges of G.
Undirected Directed (Digraph) Weighted
Graph Definitions

• |V| is the number of vertices


• |E| is the number of edges
• A digraph (directed graph) is a graph whose
edges are directed from one node i to another
one j and there is no edge from j to i.
Undirected Directed (Digraph) Weighted
Graph Definitions

• A multi-graph is a graph whose vertices can be


connected by more than one edge between the
same two vertices.
Multi-
• In the mutli-graph there is no graph

edge between any vertix and


itself.
A pseudo-graph: is a multi-graph but there is an
edge between the node and itself.
Pseudo-
graph
Graph Definitions

• A Path from v1 to vn is a sequence of edges


edge(v1, v2), edge(v2, v3),…, edge(vn-1, vn), and
is denoted as path v1, v2, v3, .. , vn.
• If v1 = vn and no edges are repeated, then the
path is called a circuit
• If all vertices in a circuit are different, then it’s
called a cycle.
• A weighted-graph has a number (weight)
assigned to each edge. This weight represents
distance, cost, length, effort, etc.
Circuit

Cycle

Chapter 6: Binary Trees 8


Graph Definitions

• In a complete graph (denoted Kn) for each pair


of distinct vertices there is exactly one edge
connecting them, that is; that is, each vertex can
be connected to any other vertex. The number
of edges in such a graph |E| = |V| * |V-1| / 2
• Two vertices vi and vj are called adjacent if the
edge(vi , vj ) is in E.
• An edge connecting to vertices is incident with
them.
Graph Definitions
• The degree of a vertex v, deg(v), is the number
of edges incident with v.
• If deg(v) = 0, then v is an isolated vertex and G
is a disconnected graph. Disconnected

• A graph G is connected if
there is a path from any vertex
to any other vertex in the
graph
Connected
• A connected graph
cannot have any
isolated vertices
Graph Theory

• Graph theory is the science of studying graphs


and their algorithms.
• It started 200 years ago.
• Numerous applications of graphs.
• Operations Research / Decision Support
• Networks
• Math / Algorithms
Some Example Applications of Graph
• Finding the least congested route between two phones, given connections
between switching stations.

• Finding the shortest path from one city to another.

• As a traveling sales-person, finding the cheapest path that passes through


all the cities that the sales person must visit.

• Determining an ordering of courses so that prerequisite courses are


always taken first.

• Networks: Vertices represent computers and edges represent network


connections between them.

• World Wide Web: Vertices represent webpages, and edges represent


hyperlinks.
• Determining if there is a way to get from one page to another, just by following links.
12
Why Graphs?

• Graphs provide the ultimate flexibility in data


structure.
• Graphs can model both real-world systems and
abstract problems, so they are used in hundreds
of applications.
• Modeling connectivity in computer and
communications networks.
• Representing a map as a set of locations with
distances between locations; used to compute
shortest routes between locations.
13 Chapter 9: Sorting
Why Graphs?

• Modeling flow capacities in transportation networks.

• Finding a path from a starting condition to a goal


condition; for example, in artificial intelligence
problem solving.
• Finding an acceptable order for finishing subtasks in a
complex activity, such as constructing large buildings.
• Modeling relationships such as family trees, business
or military organizations, etc.

14 Chapter 9: Sorting
8.1 Graph Representation

• An adjacency list is a list that specifies all the


vertices that are adjacent (connected) to each
vertex.

• An adjacency matrix of a graph G = (V, E) is a


binary |V| x |V| matrix such that each entry of the
matrix:
• aij = 1 if there exists an edge (vi vj)
0 otherwise
8.1 Graph Representation

• How many adjacency matrices are there for the


same graph?

• For a multi-graph,
• aij = is the number of edges between vi and vj
0 otherwise
8.1 Graph Representation

• Incidence Matrix for a graph G = (V,E) is a |V| x |E|


matrix such that:
• aij = 1 if edge ej is incident with vertix vi
0 otherwise

• Which implementation is better?


A Small Airline Service Map

20 Chapter 9: Sorting
A Small Airline Service Graph
A Small Airline Service Graph
Path and Cycle
Representing A Graph w an
Adjacency List
Representing A Graph w an
Adjacency Matrix

Adjacency matrix of
an undirected graph
is symmetric across
the diagonal
Directed Graph Representation
Undirected Graph Representation
Representation Costs

Adjacency Matrix:
▪ Space is O(N2)
▪ Finding the neighbors is O(N)
▪ Finding if two nodes are connected is O(1)
Adjacency List:
• Space is O(N x Dmax) or O(N + |E|)
▪ Finding the neighbors is O(Dmax)
▪ Finding if two nodes are connected is O(Dmax)
A Small Airline Service Graph w Costs
8.2 Graph Traversal

• Graph Traversal is visiting every vertex in the


graph once.
• How different is this from tree traversal?

• A graph may be disconnected.


• A graph may have cycles.
Depth-First Graph Traversal

• Depth-first search algorithm was developed by


Hopcroft and Tarjan.
• Each vertex v is visited and then each unvisited
vertex adjacent to v is visited.
• If v has no adjacent vertex or all adjacent
vertices are visited, we backtrack to the previous
vertex.
• If we backtrack to the first node and there still
unvisited vertices (disconnected graph), we pick
another unvisited vertex and start over.
Depth-First Graph Traversal
• depthFirstSearch ()
//none of the vertices is visited yet
for all vertices v
num (v) = 0
//set of edges is initially empty
edges = null
i = 1
//while there are unvisited vertices
while there is a vertex v
such that num (v) is 0
DFS (v)
output edges
Depth-First Graph Traversal
• DFS (v)
//mark that vertex v is visited
num (v) = i++
//for all neighbors of v
for all vertices u adjacent to v
//if it is not visited yet
if num (u) is 0
attach edge (uv) to edges
DFS (u)
Chapter 6: Binary Trees 34
Chapter 6: Binary Trees 35
The complexity of depthFirstSearch() is O(|V| + |E|) because (a)
initializing num(v) for each vertex v requires |V| steps; (b) DFS(v) is called
deg(v) times for each v—that is, once for each edge of v (to spawn into more
calls or to finish the chain of recursive calls)—hence, the total number of calls is
2|E|; (c) searching for vertices as required by the statement
while there is a vertex v such that num(v) is 0
can be assumed to require |V| steps. For a graph with no isolated parts, the
loop makes only one iteration, and an initial vertex can be found in one step,
although it may take |V| steps. For a graph with all isolated vertices, the loop
iterates |V| times,
and each time a vertex can also be chosen in one step, although in an
unfavorable implementation, the ith iteration may require i steps, whereby the
loop would require O(|V|2) steps in total. For example, if an adjacency list is
used, then for each v, the condition in the loop,
for all vertices u adjacent to v
is checked deg(v)times. However, if an adjacency matrix is used, then the
same condition is used |V| times, whereby the algorithm’s complexity becomes
O(|V|2).
36
8.3 Shortest Path Problem

• It is the problem of finding the shortest route


connecting a source node to a destination node.
• Applications?
• Transportation / communication
• Finding the shortest road from a city to another
(navigator devices / Mapquest / Google Maps).
• Finding the shortest path to route to direct a
data packet between two computers or a phone
call between two mobile phones.
• http://www.dgp.toronto.edu/~jstewart/270/9798s/Laffra/D
ijkstraApplet.html
Shortest Path Problem

• Applications?
• In a graph, vertices can describe states and
edges describe possible transitions.
• Shortest path algorithms can find an optimal
sequence of choices to reach a certain goal
state with the least time or effort.
• E.g., if vertices are the states of a puzzle like
Rubik's Cube and each directed edge is a single
move or turn, shortest path algorithms can be
used to find a solution that uses the minimum
possible number of moves.
Dijkstra’s Algorithm
Dijkstra (weighted simple digraph, vertex first)
for all vertices v
currDist(v) = ;
currDist(first) = 0;
tobeChecked = all vertices;
while tobeChecked is not empty
v = a vertex in tobeChecked with minimal currDist(v);
remove v from tobeChecked;
for all vertices u adjacent to v and in tobeChecked
if currDist(u) > currDist(v) + weight (edge(vu))
currDist(u) = currDist(v) + weight (edge(vu));
predecessor(u) = v;
Chapter 6: Binary Trees 40
Complexity of Dijkstra’s Algorithm

• The algorithm’s complexity is O(|V2|)


• The first for loop and while loop and executed
|V| times.
• .. .
Dijkstra's Shortest Path Algorithm

• Find shortest path from s to t.

24
2 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44
42
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }



24
2 3
0 9

s
18
14  2 6
6 
30  11
4 19

15 5
5
6
20 16

t
7 44
43
distance label  
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }

delmin


24
2 3
0 9

s
18
14  2 6
6 
30  11
4 19

15 5
5
6
20 16

t
7 44
44
distance label  
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }

decrease key



X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
30  11
4 19

15 5
5
6
20 16

t
7 44
45
distance label  15
X 
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }

delmin


X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
30  11
4 19

15 5
5
6
20 16

t
7 44
46
distance label  15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }



X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
30  11
4 19

15 5
5
6
20 16

t
7 44
47
 15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }

decrease key

 33
X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
30  11
4 19

15 5
5
6
20 16

t
7 44
48
 15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }

 33
X

X 9
24
2 3
0 9
delmin
s
18
14 
X 14
2 6
6 
30  11
4 19

15 5
5
6
20 16

t
7 44
49
 15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
44
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
50
 15
X 
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
44
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
51
 15
X delmin 
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
X 35
44
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
52
 15
X 
59 X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t } delmin

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
X 35
44
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
53
 15
X 
59 X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
X 35
44 X 34
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
54
 15
X 51 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
X 35
44 X 34
30 
X 11
4 19

15 5
5
6
20 16
delmin

t
7 44
55
 15
X 51 59
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
56
 15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 11
4 19

15 5 delmin
5
6
20 16

t
7 44
57
 15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
58
 15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
59
 15
X
delmin 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
60
 15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }

32
 33
X X

X 9
24
2 3
0 9

s
18
14 
X 14
2 6
6 
45 X
X 35
44 X 34
30 
X 11
4 19

15 5
5
6
20 16

t
7 44
61
 15
X 50 51
X 59 
X X
Chapter 6: Binary Trees 62
8.5 Minimum Spanning Tree

• .. ..
Chapter 6: Binary Trees 64
What is A Spanning Tree?
• A spanning tree for an
undirected graph G=(V,E)
is a subgraph of G that is a
tree and contains all the a
vertices of G
b u e

• Can a graph have more


than one spanning tree? c v f

• Can an unconnected graph d


have a spanning tree?

Greedy/Prims 65
Example of a Problem that Translate into
a MST
The Problem
• Several pins of an electronic circuit must be
connected using the least amount of wire.

Modeling the Problem


• The graph is a complete, undirected graph
G = ( V, E ,W ), where V is the set of pins, E is the set
of all possible interconnections between the pairs of
pins and w(e) is the length of the wire needed to
connect the pair of vertices.
• Find a minimum spanning tree.

Greedy/Prims 66
Greedy Choice

There are two ways to build a minimum spanning tree.


• A MST can be grown from the current spanning tree
by adding the nearest vertex. (Prim's algorithm)

• A MST can be grown from a forest of spanning trees


by adding the smallest edge. (Kruskal's algorithm)

Greedy/Prims 67
3. Minimum Spanning Tree Problem
◼ Example of Prim’s Algorithm
2
2 1 2
1 2 3 6
2 3 6 2
1 2 1 2 5 8 4
3 6 5 8 4 3 6
1 3
1 3
5 8 4 5 8 4
7 2
1 3 6 7 4 2
3 1 3 6 7
4
9 3
7 9
7 2 7 2 2
6 4 3 2 6 7
4
9 3 2
7 9 2 8 6 8
6
2 1 2 1
2 8 5 4 6 2 8 5 4
6
1 1
5 4 5 4
Select a node
at random
2
2 1 2
1 2 3 6
2 3 6 2
1 2 1 2 5 8 4
3 6 5 8 4 3 6
1 3
5 8 4 1 3
5 8 4 7 2
1 3 7 2 6 7
4
9 3
6 7
4
9 3 1 3
7 2 2
6 7
4
9 3 2 6 7 4 2
3 2 8
2 8 7 9 6
6
2 2 1
6 2 8 1 2 8
5 4
5 4 6
5 1 4 5 1 4
Soft Computing Lab. WASEDA UNIVERSITY , IPS 71
f (x*)= 18

You might also like