Date post: | 22-Dec-2015 |
Category: |
Documents |
View: | 214 times |
Download: | 0 times |
Graphs
• One limitation of the trees is that the trees are hierarchal in nature. They only represent relations of hierarchal types such as relations between parents and child
• A generalization of tree, called GRAPH, is a data structure in which these limitations are lifted
• A graph is a collection of vertices (nodes) and connection between them called edges
• A simple graph G= (V,E) consists of a nonempty set of V of vertices and possibly empty set E of edges where each edge is a connection between two vertices
• The number of vertices and edges of a graph are represented as |V| and |E|, respectively.
Examples of Simple Graphs
Graph 1 Graph 2
Graph 3 Graph 4
Diagraphs (Directed Graphs)
• A directed graph (diagraph) G(V,E) consists of a non-empty set of V vertices and a set of edges (arcs) where each edge is a pair of vertices from V
• The difference between diagraph and simple graphs is that edge {vi ,vj} = {vj ,vi} in a simple graph but this is not the case in a diagraph
• Here is an example of a diagraph
Multi-graphs • In a simple graph, two vertices can only be joined by one edge
• However, in a multi-graph more than one edge can connect the same two vertices together.
• A multi-graph that has an edge going from vertex vi to the same vertex vi (loop edge) is called pseudo-graph
Path:• A path from v1 to vn is a sequence of edges edge(v1,v2),
edge(v2, v3), … (vn-1, vn)
Cycle:• If v1 = vn and no edge is repeated then it is called a cycle
v1 v2
v3
v5
v4
v6
v1 v2
v3
v5
v4
v6
In the following graph, the edges that connects v1 to v2 and v2 to v3 and v3 to v5 makes a path from v1 to v5.
The path going from v1 to v2 and from v2 to v3 and from v3 to v1 again makes a cycle.
Weighted Graph:• A graph is called weighted graph if each edge has an
assigned number• Depending on the context in which such a graphs are
used, the number assigned to an edge is called its weight, cost, distance, etc.
Complete Graph:• A graph with n vertices is called “complete” and is denoted as kn if for each pair of distinct vertices exactly one edge connecting them.• That is each vertex can be connected to any other vertex• The number of edges in such a graph is:
|V| |E| = = [ |V| * (|V|-1) / 2] which is O(|V|2)
2
k4
Sub-graph: • A sub-graph G’ of graph G=(V, E) is a graph G’=(V’, E’)
such that V’ is a subset of V and E’ is a subset of E . A Sub-graph induced by vertices V’ is a graph (V’, E’) such that if an edge e is in E’, then e is in E
Adjacent:• Two vertices vi and vj are called adjacent if the edge (vi,vj)
is in E. Such an edge is called incident with the vertices vi and vj
Degree:• The degree of a vertex v, deg(v) is the number of edges
incident with v. If deg(v)=0, then v is called isolated vertex.
Graph Representation• Graph can be represented
(implemented) in several ways.
• A simple representation is given by adjacency list that specifies all vertices adjacent to each vertex of a graph
• This is called STAR representation
b
c
d
e
f
f
a c d
d e
f
a f
ea b
b d
da c
gc
a
f
d
b
e
g
Matrix Representation
• Another common representation is a Adjacency matrix• An adjacency matrix of graph G= (V, E) is a binary |V|*|V|
matrix such that for each entry of this matrix A[i, j] • A[i, j] = 1 if there exist an edge between vertex vi and vj
• Otherwise A[i, j] = 0
c
a
f
d
b
e
g 0000000
0001101
0001010
0110011
0100001
0011000
0101100
a b c d e f ga bc d e f g
Comparison:
• Which representation is better?
• Think about searching for an edge
• Think about printing all edges
• Think about inserting an edge
• How about inserting a vertex
• How about deleting a vertex
• How about deleting an edge
Graph Traversal
• Two systematic method for traversal of graph are depth-first traversal and breadth-first traversal
• Of course since graph may contain cycle, they have different structure compare to trees
• Therefore, traversal algorithms we used for trees cannot be applied for graphs exactly the same way and they should be modified to fit the graph requirements
• Further, not all graphs are connected. A graph may contain isolated trees, isolated graphs or isolated vertices.
• In a connected graph, we can find a path between any two vertices in the graph but this is not the case for unconnected graphs
Depth-First Traversal Using Recursion
• A vertex v is visited once then each adjacent vertex to v is visited and so on
• If vertex v has no adjacent vertex or all of its adjacent vertices have already been visited, we backtrack to the predecessor of v
• The traversal is finished if this visiting and backtracking process leads to the first vertex where the traversal started
• Finally, if there is still some unvisited vertex in the graph, the traversal continues for one of the unvisited vertices
DFS(v)num(v) = i++ // making vertex v visitedfor all vertices u adjacent to v
if num(u) is 0 // it means it is not visited yetattach edge(uv) to edgesDFS(u);
depthFirstSearch()for all vertices v
num(v) = 0; // initializing all vertices to unvisitededges = null;i = 1;while there is a vertex v such that num(v) is 0
DFS(v);output edges;
• Note that this algorithm guarantees generating a tree (or a forest (combination of several smaller trees) which includes or spawns over all vertices of the original graph
• The tree that meets these condition is called a spanning tree
• Also note that an edge is added to edges only if the condition in “if num(u) is 0” is true; that is if vertex u reachable from vertex v has not been processed
• Therefore, certain edges in the original graph do not appear in the resulting tree
• The edges included in the tree are called forward edges (or tree edges) and the ones not included are called back edges
• In the next example, we show back edges with dash lines and forward edges with straight line
c
a
i
f
b
g
e
h
d
Original Graph
c
a(1)
i
f
b
g
e
h
d • We pick up a vertex randomly and mark it as visited
• Lets pick vertex a and mark it as visited
Example of Depth-First Search
c(2)
a(1)
i
f
b
g
e
h
d
• The adjacent vertices to a are c, f, g and i
• Any of the adjacent vertices to a can be chosen randomly as long as it is not visited already
• Lets pick c. Mark c as visited and add edge ac to the edges
• The adjacent vertices to c are a, f and i
• Vertex a cannot be chosen because it is already visited. But vertex f or i can be chosen
• Lets pick f. Mark f as visited and add edge cf to the edges
c(2)
a(1)
i
f(3)
b
g
e
h
d
• The adjacent vertices to f are a, c and i
• Vertex a and c cannot be chosen because they are already visited. So the only choice is vertex i
• Mark i as visited and add edge fi to the edges
c(2)
a(1)
i(4)
f(3)
b
g
e
h
d
• The adjacent vertices to i are a, c and f but they are all visited
• We backtrack to f and vertices adjacent to f are all visited.
• We backtrack to c and vertices adjacent to c are also visited
• We backtrack to a and we find out that g, an incident edge to a, is not visited
• Mark g as visited and add edge ag to the edges
c(2)
a(1)
i(4)
f(3)
b
g(5)
e
h
d
c(2)
a(1)
i(4)
f(3)
b(6)
g(5)
e
h
d
• The adjacent vertices to g are a and b
• Vertex a cannot be chosen because it is already visited. So the only choice is vertex b
• Mark b as visited and add edge gb to the edges
c(2)
a(1)
i(4)
f(3)
b(6)
g(5)
e(7)
h
d
• The adjacent vertices to b is g, but g is already visited
• We backtrack to g and vertices adjacent to g are all visited.
• We backtrack to a but all adjacent vertices to a are also visited
• Since a is where we started, we cannot backtrack anymore, so we pick another unvisited vertex if there is still one
• Lets pick e and mark e as visited.
• The only adjacent vertex to e is h
• Mark h as visited and add edge eh to the edges
c(2)
a(1)
i(4)
f(3)
b(6)
g(5)
e(7)
h(8)
d
• The adjacent vertices to h are e and d
• Vertex e cannot be chosen because it is already visited. So the only choice is vertex d
• Mark d as visited and add edge hd to the edges
c(2)
a(1)
i(4)
f(3)
b(6)
g(5)
e(7)
h(8)
d(9)
c(2)
a(1)
i(4)
f(3)
b(6)
g(5)
e(7)
h(8)
d(9)
• The adjacent vertices to d is h, but h is already visited
• We backtrack to h and adjacent vertices to h are all visited.
• We backtrack to e and adjacent vertices to e are also visited
• Since e is where we started, we cannot backtrack anymore, so we pick another unvisited vertex if there is still one
• But there is no more vertex left to pick. Therefore, we are done
c(2)
a(1)
i(4)
f(3)
b(6)
g(5)
e(7)
h(8)
d(9)
The final result is:
Another example using Directed Graph
c
a
i
f
b
g
e
h
d
Original Graph
• We pick up a vertex randomly and mark it as visited
• Lets pick vertex a and mark it as visited
c
a(1)
i
f
b
g
e
h
d
c(2)
a(1)
i
f
b
g
e
h
d• The adjacent vertices to a are c,
and f
• Any of the adjacent vertices to a can be chosen randomly as long as it is not visited already
• Lets pick c. Mark c as visited and add edge ac to the edges
c(2)
a(1)
i(3)
f
b
g
e
h
d • The only adjacent vertices to c is i
• Mark i as visited and add edge ci to the edges
c(2)
a(1)
i(3)
f(4)
b
g
e
h
d
• The only adjacent vertex to i is a which is already visited
• We backtrack to c and the only vertex adjacent to c is also visited.
• We backtrack to a and we find out that f is not visited
• Mark f as visited and add edge af to the edges
c(2)
a(1)
i(3)
f(4)
b(5)
g
e
h
d
• The adjacent vertices to f are c and i which are already visited
• We backtrack to a and all adjacent vertices to a are also visited
• Since we started with a, we cannot backtrack anymore. So we pick another unvisited vertex if there exist one
• Lets pick b and mark it as visited
c(2)
a(1)
i(3)
f(4)
b(5)
g(6)
e
h
d• The only adjacent vertex to b
is g
• We mark g as visited and add bg to the edges
c(2)
a(1)
i(3)
f(4)
b(5)
g(6)
e(7)
h
d
• The only adjacent vertex to g is a which is already visited
• We backtrack to b and the adjacent vertex to b is also visited
• Since we started with b, we cannot backtrack anymore. So we pick another unvisited vertex if there exist one
• Lets pick e and mark it as visited
c(2)
a(1)
i(3)
f(4)
b(5)
g(6)
e(7)
h(8)
d • The only adjacent vertex to e is h
• We mark h as visited and add eh to the edges
c(2)
a(1)
i(3)
f(4)
b(5)
g(6)
e(7)
h(8)
d(9) • The only adjacent vertex to h is d
• We mark d as visited and add hd to the edges
• There is no adjacent vertex to d
• We backtrack to h and the adjacent vertex to h is already visited.
• We backtrack to e and the adjacent vertex to e is also visited
• Since e is where we started, we cannot backtrack anymore, so we pick another unvisited vertex if there is still one
• But there is no more vertex left to pick. Therefore, we are done
c(2)
a(1)
i(3)
f(4)
b(5)
g(6)
e(7)
h(8)
d(9)
c(2)
a(1)
i(3)
f(4)
b(5)
g(6)
e(7)
h(8)
d(9)
The final result is:
Complexity of depth-first search
• The complexity of depthFirstSearch() is O(|V| + |E|) because:
• Initializing num(v) for each vertex requires |V| steps
• 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| because each edge is adjacent to two vertices
• Searching for vertices as required by the statement While there is vertex v such that num(v) is 0
• Can be assumed to require |V| steps in worse case.
Breadth-First Search• This type of traversal uses queue instead of recursion or stack
breathFirstSearch()for all vertices u
num(u) =0;edges = null;i = 1;while there is a vertex such that num(v) = 0
num(v) = i++;enqueue(v); // pushing into queuewhile queue is not empty v = dequeue(); // popping from the queue for all vertices u adjacent to v if num(u) is 0
num(u) = i++;enqueue(u);add edge(vu) to edges;
Output edges;
Original Graph
c
a(1)
i
f
b
g
e
h
d • We pick up a vertex randomly and mark it as visited
• Lets pick vertex a and mark it as visited and place it in the queue
Example of Breadth-First Search
a
c
a
i
f
b
g
e
h
d
c(2)
a(1)
i(3)
f(4)
b
g(5)
e
h
d
• We pop a from the queue.
• The adjacent vertices to a are g, f, i and c that are not visited
• We mark them all one by one and place them in the queue (order is not important)
• The related edges are added
c
i
f
g
c(2)
a(1)
i(3)
f(4)
b
g(5)
e
h
d• We pop c from the queue.
• All adjacent vertices to c are visited already.
• So no vertex goes into the queue
i
f
g
c(2)
a(1)
i(3)
f(4)
b
g(5)
e
h
d • We pop i from the queue.
• All adjacent vertices to i are visited already.
f
g
c(2)
a(1)
i(3)
f(4)
b
g(5)
e
h
d • We pop f from the queue.
• All adjacent vertices to f are visited already.
g
c(2)
a(1)
i(3)
f(4)
b(6)
g(5)
e
h
d • We pop g from the queue.
• The only adjacent vertex to g that is not visited is b
• We mark b and place it into the queue
• We add edge gb b
c(2)
a(1)
i(3)
f(4)
b(6)
g(5)
e
h(7)
d• We pop b from the queue
but adjacent vertex to b is visited already
• The queue become empty
• We see if there is any unvisited vertex
• We pick h, mark it and place it in the queueh
c(2)
a(1)
i(3)
f(4)
b(6)
g(5)
e(8)
h(7)
d(9)• We pop h from the queue
• Adjacent vertices to h are e and d
• We mark them and place them in the queue
• We also add the related edgesd
e
c(2)
a(1)
i(3)
f(4)
b(6)
g(5)
e(8)
h(7)
d(9)• We pop e
• Adjacent vertices to e are all visited already
d
c(2)
a(1)
i(3)
f(4)
b(6)
g(5)
e(8)
h(7)
d(9)• We pop d
• Adjacent vertices to d are all visited already
• Nothing more is left to pop.
• No more unvisited vertex exist
• Therefore, we are done
The final result is:
c(2)
a(1)
i(3)
f(4)
b(6)
g(5)
e(8)
h(7)
d(9)
Finding Shortest Path
• Finding the shortest path is a classical problem in graph theory
• Edges of a graph are assigned certain weights representing for example,
• Distance between cities• Times separating the execution of programs• Costs of transmitting data from one site to another
• When determining the shortest path from vertex v to vertex u, information about distances between intermediate vertices w, has to be recorded
• Several algorithms have been proposed to solve the shortest path problem
• In this course we discuss 3 algorithms commonly used to find the shortest path:
• Dijkstra Algorithm• Ford Algorithm• WFI Algorithm (created by R. Floyed and P. Ingerman)
• Dijkstra and Ford algorithms find shortest path from vertex v to any other vertex in the graph.
• WFI Algorithm finds shortest path from any vertex to any other vertices
Dijkstra Algorithm: Finding Shortest Path
• This algorithm find the shortest path from vertex called first to any other vertex in a directed graph
DijkstraAlgorith(weighted simple diagraph, vertex first) for all vertices v currDist(v) = infinity; currDist(first) = 0; toBeChecked = all vertices;
while toBeChecked is not emptyv = a vertex in toBeChecked with minimal currDist(v);remove v from toBeChecked;
for all vertices u adjacent to v and in toBeCheckedif (currDist(u) > currDist(v) + weight(edeg(vu) )
currDist(u) = currDist(v) + weight(edge(vu));Predecessor(u) = v;
• In this algorithm, we can create two arrays: • predecessor array to keep track of the predecessor of
the vertices • For example if the shortest path from vertex a to vertex b is: [a, v1, v2, ….. vn, b], then the predecessor of b is vn.
• CurrDist array to keep track of the current distances of each vertex to vertex “first”
• To start, based on the algorithm, we start from the source (vertex that is called first in the algorithm)
• Then using the weigh of the edges with adjacent vertices, we update the current Distance of adjacent vertices
• Then we choose the vertex with smallest current Distance and repeat the process
• We continue this process until all vertices are checked
• We try to find the shorted path from any vertex to vertex d in this example
• We start with the source vertex, vertex d, and move forward. • The adjacent vertices to d are a and h. • Moving from d to a makes the current distance of a to be 0+4=4
• This is accepted because 4 is less than infinity (current distance of a)• This makes the predecessor of a to be d
• Also, moving from d to h makes the distance of h to be 0+1=1• This is accepted because 1 is less than infinity (current distance of h)• This makes the predecessor of h to be d
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
Null
NullNull
Null
NullNull
NullNull
NullNull
• Next we pick the vertex with the smallest current distance that is not checked• As shown in the array, this vertex is h• The adjacent vertices to h are e and i. • Moving from h to e makes the current distance of e to be 1+5=6
• This is accepted because 6 is less than infinity (current distance of e)• This makes the predecessor of e to be h
• Also, moving from h to i makes the distance of i to be 1+9=10• This is accepted because 10 is less than infinity (current distance of i)• This makes the predecessor of i to be h
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
NullNull
d
NullNull
NullNull
NullNull
4
1
Checked
• Next we pick the vertex with the smallest current distance that is not checked• As shown in the array, this vertex is a• The adjacent vertices to a are h and e. • Moving from a to e makes the current distance of e to be 4+1=5
• This is accepted because 5 is less than 6 (current distance of e)• This makes the predecessor of e to be a
• Also, moving from a to h makes the distance of h to be 4+10=14• This is not accepted because 14 is greater than 1 (current distance of h)
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
NullNull
d
hNull
Null
Null
h
Null4
1
6
10
Checked
Checked
• Next we pick the vertex with the smallest current distance that is not checked
• As shown in the array, this vertex is e
• The only adjacent vertex to e is f.
• Moving from e to f makes the current distance of f to be 5+3=8• This is accepted because 8 is less than infinity (current distance of f)• This makes the predecessor of f to be e
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
NullNull
d
hNull
NullNull
a
Null4
1
5
10
Checked
Checked
Checked
• Next we pick the vertex with the smallest current distance that is not checked• As shown in the array, this vertex is f• The adjacent vertices to f are b, c, g, and i. • Moving from f to b makes the current distance of b to be 8+1=9
• This is accepted because 9 is less than infinity (current distance of b)• This makes the predecessor of b to be f
• Moving from f to i makes the current distance of i to be 8+1=9• This is accepted because 9 is less than 10 (current distance of i)• This makes the predecessor of i to be f
• Moving from f to g makes the current distance of g to be 8+7=15• This is accepted because 15 is less than infinity (current distance of g)• This makes the predecessor of g to be f
• Moving from f to c makes the current distance of c to be 8+3=11• This is accepted because 11 is less than infinity (current distance of c)• This makes the predecessor of c to be f
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
eNull
d
hNull
NullNull
a
Null4
1
5
10
Checked
Checked
Checked
Checked
8
• Next we pick the vertex with the smallest current distance that is not checked
• As shown in the array, this vertex is either b or i. We can pick any of them. So we select b.
• The only adjacent vertex to b is c.
• Moving from b to c makes the current distance of c to be 9+2=11• This is not accepted because 11 is not less than 11 (current distance of c)
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
ef
d
fNull
Nullf
a
f4
1
5
9
Checked
Checked
Checked
Checked
8 Checked
15
119
• Next we pick the vertex with the smallest current distance that is not checked
• As shown in the array, this vertex is i.
• The only adjacent vertex to i is j.
• Moving from i to j makes the current distance of j to be 9+2=11• This is accepted because 11 is less than infinity (current distance of j)• This makes the predecessor of j to be i
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
ef
d
fNull
Nullf
a
f4
1
5
9
Checked
Checked
Checked
Checked
8 Checked
15
119 Checked
• Next we pick the vertex with the smallest current distance that is not checked
• As shown in the array, this vertex is either c or j. We select c
• There is no adjacent vertex to c
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
ef
d
fi
Nullf
a
f4
1
5
9
Checked
Checked
Checked
Checked
8 Checked
15
119 Checked
Checked
11
• Next we pick the vertex with the smallest current distance that is not checked
• As shown in the array, this vertex is j.
• The only adjacent vertex to j is g.
• Moving from j to g makes the current distance of g to be 11+1=12• This is accepted because 12 is less than 15 (current distance of g)• This makes the predecessor of g to be j
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
ef
d
fi
Nullf
a
f4
1
5
9
Checked
Checked
Checked
Checked
8 Checked
15
119 Checked
Checked
11
Checked
• Next we pick the vertex with the smallest current distance that is not checked
• As shown in the array, this vertex is g.
• There is no adjacent vertex to g
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
ej
d
fi
Nullf
a
f4
1
5
9
Checked
Checked
Checked
Checked
8 Checked
12
119 Checked
Checked
11
Checked
Checked
• All vertices are now checked. There is nothing left and we are done.• The CurrDist array shows us the minimum distance from d to any vertex• The predecessor array show us how to find the path from d to any vertex. For
example to find the path from d to j,• We start from j and find the predecessor of j which is i• then find the predecessor of i which f• then find the predecessor of f which is e• then find the predecessor e which is a• and finally the predecessor of a which is d
• Therefore the path from d to j isd a e f i j
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
abcdefghij
abcdefghij
PredecessorCurrDist
0
d
ej
d
fi
Nullf
a
f4
1
5
9
Checked
Checked
Checked
Checked
8 Checked
12
119 Checked
Checked
11
Checked
Checked
Checked
• You can show the steps of Dijkstra Algorithm all in one page as shown below
12
1
99101010
11
15
11
15
11
15
11
15
8
56
0
11
9
44 d h a e f b i c j g a
bc de f g
h
i
j
init 1 2 3 4 5 6 7 8 9 10
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
31
2
3
7
1
2
1
Complexity of Dijkstra Algorithm
• The complexity of Dijkstra algorithm is O(|V2|)
• The first for loop and the while loop are executed |V| times
• For each iteration of the while loop • A vertex in toBeChecked list with minimal current
distance has to be found which requires O(|V|) steps• The for loop iterates deg(v) times which is also O(|V|)
• Problem:• Dijkstra algorithm only works for positive weights and
it fails when negative weights are used in graphs
Ford Algorithm
• Like Dijkstra Algorithm, Ford Algorithm uses the same method for setting the current distances
• However, Ford Algorithm does not permanently determine the shortest distance for any vertex until it processes the entire graph
• Ford Algorithm, accepts both positive and negative weights but does not accept graphs with negative cycles
• The pseudocode is:
FordAlgorithm(weighted simple diagraph, vertex first)for all vertices v
currDist(v) = infinity;currDist(first) = 0;while there is an edge(vu) such that currDist(u) > currDist(v) + weight(edge(vu))
currDist(u) = currDist(v) + weight(edge(vu));
c
h
d
b
e
a
f
gi-1
1
14
-11
-521
4
11
The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if a
bcdefghi
0
Apply ababcdefghi
0
Apply beabcdefghi
0
Apply cdabcdefghi
0
Apply cg
abcdefghi
0
Apply chabcdefghi
0
Apply daabcdefghi
0
Apply deabcdefghi
0
Apply diabcdefghi
0
Apply efabcdefghi
0
Apply gdabcdefghi
0
Apply hgabcdefghi
0
Apply if
1
1
1
1
1
3
1
1
5
3
1
12
3
15
1
9
3
15
12
1 1 1 1 1 1
3
059
12
3
0
059
12
0
53
12
0
3
First Iteration
c
h
d
b
e
a
f
gi-1
1
14
-11
-521
4
11
The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if
Second Iteration
abcdefghi
0
Apply ab
0
53
12
0
34
abcdefghi
0
Apply be
0
-13
12
0
34
abcdefghi
0
Apply cd
0
-13
12
0
34
abcdefghi
0
Apply cg
0
-13
12
0
34
abcdefghi
0
Apply ch
0
-13
12
0
34
abcdefghi
0
Apply da
0
-13
12
0
24
abcdefghi
0
Apply de
0
-13
12
0
24
abcdefghi
0
Apply di
0
-13
11
0
24
abcdefghi
0
Apply ef
0
-13
11
0
24
abcdefghi
0
Apply gd
0
-13
11
-1
24
abcdefghi
0
Apply hg
0
-13
11
-1
24
abcdefghi
0
Apply if
0
-12
11
-1
24
c
h
d
b
e
a
f
gi-1
1
14
-11
-521
4
11
The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if
Third Iteration
abcdefghi
0
Apply ab
0
-12
11
-1
23
abcdefghi
0
Apply be
0
-22
11
-1
23
abcdefghi
0
Apply cd
0
-22
11
-1
23
abcdefghi
0
Apply cg
0
-22
11
-1
23
abcdefghi
0
Apply ch
0
-22
11
-1
23
abcdefghi
0
Apply da
0
-22
11
-1
13
abcdefghi
0
Apply de
0
-22
11
-1
13
abcdefghi
0
Apply di
0
-22
10
-1
13
abcdefghi
0
Apply ef
0
-22
10
-1
13
abcdefghi
0
Apply gd
0
-22
10
-1
13
abcdefghi
0
Apply hg
0
-22
10
-1
13
abcdefghi
0
Apply if
0
-21
10
-1
13
c
h
d
b
e
a
f
gi-1
1
14
-11
-521
4
11
The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if
Fourth Iteration
abcdefghi
0
Apply da
0
-31
10
-1
12
abcdefghi
0
Apply ab
0
-21
10
-1
12
abcdefghi
0
Apply be
0
-31
10
-1
12
abcdefghi
0
Apply cd
0
-31
10
-1
12
abcdefghi
0
Apply cg
0
-31
10
-1
12
abcdefghi
0
Apply ch
0
-31
10
-1
12
abcdefghi
0
Apply de
0
-31
10
-1
12
abcdefghi
0
Apply di
0
-31
10
-1
12
abcdefghi
0
Apply ef
0
-31
10
-1
12
abcdefghi
0
Apply gd
0
-31
10
-1
12
abcdefghi
0
Apply hg
0
-31
10
-1
12
abcdefghi
0
Apply if
0
-31
10
-1
12
c
h
d
b
e
a
f
gi-1
1
14
-11
-521
4
11
The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if
Fifth Iteration
abcdefghi
0
Apply ch
0
-31
10
-1
12
abcdefghi
0
Apply if
0
-31
10
-1
12
abcdefghi
0
Apply ab
0
-31
10
-1
12
abcdefghi
0
Apply be
0
-31
10
-1
12
abcdefghi
0
Apply cd
0
-31
10
-1
12
abcdefghi
0
Apply cg
0
-31
10
-1
12
…….. ……… ………… …… ……… ……… …… …….
Note: In this iteration nothing changes. This is the sign that shows we are done and have found the minimum distances
• In the example of the Ford algorithm, we ordered the edges alphabetically. However, the order of the edges should have no effect on the final result
• In this example, we showed that we do as many as iterations as required in order to find the minimum distance from the source vertex (called first in the algorithm) to any other vertex.
• Note that the current distance of some vertices changed more than once even within the same iteration
Complexity of Ford Algorithm
• The computational complexity of this algorithm is O(|V|*|E|) because
• there would be at most |V| - 1 passes through the sequence of |E| edges since |V| -1 is the largest number of edges in any path
• in the first pass, at least one-edge paths are determined
• in the second pass, all two-edges paths are determined
• and so on
• You can show the steps of Ford Algorithm all in one page as shown below
1
0 122
01
1239
-3-2-155
-101
0
234
1233abc de f g
h
i
Iterationsinit 1 2 3 4
c
h
d
b
e
a
f
gi-1
1
14
-11
-521
4
11
The edges are: ab, be, cd, cg, ch, da, de, di, ef, gd, hg, if
All to All Shortest Path
• Dijkstra and Ford Algorithms only determine the shortest path from a particular vertex to other vertices in the graph
• Robert W, Floyed and P.Z Ingerman introduced an algorithm called WFIAlgorithm that determine the shortest path from any vertex vi to any other vertex vj
• The pseudocode is:
WFIAlgorithm (matrix weight)for i =1 to |V|
for j = 1 to |V|for k = 1 to |V|
if weight[j][k] > weight[j][i] + weigh[i][k] weight[j][k] = weight[j][i] + weigh[i][k];
• In the WFI algorithm, the outer loop refers to the vertices which may be on a path between the vertex with the index j and the vertex with index k
• For example, in the first iteration, when i=1, all paths vj …. v1, ….., vk are considered, and if there is currently no
path from vj to vk and vk is reachable from vj, the path is established with its weight equal to p where p is:
p = weight(path(vj, v1)) + weight (path(v1, vk)) or current weight of this path, weight(vj,vk) is changed to p
if p is less than weight(path(vj,vk))
b
d
a
e
c-4
2
1
41
3
-2
Step 1:
We show how to go from a vertex specified in a row to a vertex specified in a
column via vertex a
0
4 0
10
31-2 0
-420abc de
a b c d e b
d
a
e
c
b
d
a
e
c-4
2
1
41
3
-2
Step 2:
We show how to go from a vertex specified in a row to a vertex specified in a
column via vertex b
0
4 0
10
31-2 0
5-4020abc de
a b c d e
b
d
a
e
c
The red arrows mean: - it is cheaper to go from a to c via b - Also it is cheaper to go from a to e via b
b
d
a
e
c-4
2
1
41
3
-2
Step 3:
We show how to go from a vertex specified in a row to a vertex specified in a
column via vertex c
0
4 0
10
-11-2 0
1-4020abc de
a b c d e
b
d
a
e
c
The red arrows mean: - it is cheaper to go from a to c via b - Also it is cheaper to go from b to e via c - and so far cheapest path from a to e is
a b c e
b
d
a
e
c-4
2
1
41
3
-2
Step 4:
We show how to go from a vertex specified in a row to a vertex specified in a
column via vertex d
0
4 0
10
-11-2 0
0-4020abc de
a b c d e
b
d
a
e
c
The red arrows mean: - it is cheaper to go from a to c via b - Also it is cheaper to go from b to e via c - Also it is cheaper to go from a to e via d
Spanning Trees• Consider a graph that represents airline connection
• If economic situation forces this airline to shut down as many connection as possible, which of them should be retained to make sure that it is still possible to reach any city from other cities
• The point is to find a solution that allows us to remove as many edges as possible such that any node can be reached from any other node
• You may be able to find several solutions to this problem; however, if there is cost involved in going from node A to node B , then we prefer to minimize the number of connections with the lowest cost possible
• This type of problem is called finding Minimum Spanning Tree (MST) which is a Spanning Tree of a graph in which the scan of the weights of its edges is minimum
• There are several algorithms to find MST. Some well-known algorithms are discussed in this course
• Boruvka’s algorithm• Kruskal’s Algorithm• Jannik Prime’s Algorithm• Dijkstra’s Algorithm
b
c
a
e
d
g
f
b
c
a
e
d
g
f
b
c
a
e
d
g
f
Solution 1 Solution 2
Boruvka’s algorithm: Finding MST Proposed in 1926
• In this method• start with |V| on-vertex trees • For each vertex v, we look for an edge (vw) of minimum
weight among all edges outgoing from v and create small trees by including these edges
• Look for edges of minimal weight that can connect the resulting trees of large trees
• The process is finished when one tree is created
• The pseudocode is:
BoruvkaAlgorithm (weighted connected undirected graph) make each vertex the root of one-node tree while there is more than one tree for each tree t e = minimum weight edge (vu) where v is included in t and u is not create a tree by combining t and the tree that includes u if such a
tree does not exist yet
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13• We first start with |V| vertices that are 7 isolated trees
• For each tree we look for an edge with minimum weight uv where u and v belong to two different trees
• For a we pick ac• For b we pick ba• For c we pick ca (same as ac)• For d we pick df• For e we pick eg• For f we pick fg• For g we pick gf (same as fg)
• The result is shown in here
b
c
a
e
d
g
f
5
6
8
7
3
First Iteration: (step 1) Original graph
• We are not done yet because we still have more than one tree: t1 and t2
• For each tree we look for an edge with minimum weight uv where u and v belong to two different trees
• For t1, our options are cf, be, and cd but we pick cf because cf has the minimum weight
• Similarly, for t2, our options are fc, eb, and dc but we pick fc because fc has the minimum weight
• The result is shown in here
• This is the final result because we now have only one tree
• What is the runing time of this algorithm?
b
c
a
e
d
g
f
5
6
8
7
3
Second Iteration: (step 2) t1
t2
12
b
c
a
e
d
g
f
5
6
8
7
3
12
1316
Kruskal’s algorithm: Finding MST
• In this method• Edges are ordered by weight in ascending order• Each edge in this ordered sequence is checked to see if it can
be considered part of the tree construction• The edge is added if no cycle is created after its inclusion
• The pseudocode is:
KruskalAlgorithm (weighted connected undirected graph) tree = null;
edges = sequence of all edges of graph sorted by weight; for each edge uv in the sorted edge list if adding uv does not cause a cycle with other edges in the tree then
add uv to tree
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13• We find the first edge with minimum weight
• This edge is gf with weight 3
• So we add gf
gf, ac, ab, df, eg, bc, cf, be, de, cd
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13• We find the next edge with
minimum weight
• This edge is ac with weight 5
• We add ac because it does not cause a cycle
gf, ac, ab, df, eg, bc, cf, be, de, cd
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13• We find the next edge with
minimum weight
• This edge is ab with weight 6
• We add ab because it does not cause a cycle
gf, ac, ab, df, eg, bc, cf, be, de, cd
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13• We find the next edge with minimum weight
• This edge is fd with weight 7
• We add fd because it does not cause a cycle
gf, ac, ab, fd, eg, bc, cf, be, de, cd
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13• We find the next edge with minimum weight
• This edge is eg with weight 8
• We add eg because it does not cause a cycle
gf, ac, ab, df, eg, bc, cf, be, de, cd
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13• We find the next edge with
minimum weight• This edge is cb with weight 9• We cannot add cb because cb
causes cycle in the tree• The next one is cf, with weight 12• We add cf because cf does not
cause cycle
gf, ac, ab, df, eg, cb, cf, be, de, cd
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5
6
8
127
3
• Now we only have one connected tree and adding any more edge (be, de, and cd) causes cycle
• Thus, we are done
• Can you find the running time of Kruskal Algorithm?
gf, ac, ab, df, eg, cb, cf, be, de, cd
Jarnik Prim’s Algorithm : Finding MST Proposed in 1936
• In this method• Edges are ordered by weight in ascending order• Add an edge to the spanning tree with the minimum cost if
• The inclusion makes no cycle and• The edge is incident to a vertex that is already in the
spanning tree (i. e. the spanning tree grows as one connected tree)
• The pseudocode is:JarnikPrimeAlgorithm (weighted connected undirected graph) tree = null;
edges = sequence of all edges of graph sorted by weight; for i = 1 to |V| -1
for j= 1 to |edges| if adding uv does not cause a cycle with other edge in the spanning
tree and uv is incident to a vertex that is in the spanning tree then add uv to the spanning tree
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13• We find the first edge with
minimum weight
• This edge is fg with weight 3
• So we add fg first
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next we find the edges incident to gf.
• They are, ge, fc and fd
• We pick fd because it has minimum weight and causes no cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next we find the edges incident to vertices of the current spanning tree
• They are ge, fc, de, and dc.
• We pick ge because it has minimum weight and causes no cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Again we find the edges incident to vertices of the current spanning tree
• They are ed, eb, fc, and dc
• We pick fc because it has minimum weight and causes no cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Again we find the edges incident to vertices of the current spanning tree
• They are ca, cb, cd, eb, and ed
• We pick ca because it has minimum weight and causes no cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Again we find the edges incident to vertices of the current spanning tree
• They are ab, cb, cd, eb, and ed
• We pick ab because it has minimum weight and causes no cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5
6
8
127
3
• Other edges are cb, cd, eb, and ed.
• Addition of any of these edges causes cycle
• Therefore, we are done
• What is the running time?
Dijkstra’s Algorithm : Finding MST
• In this method no sorting is required• Pick up an edge from unsorted list and add it to the tree• If cycle occurs, remove the edge with maximum weight to break
the cycle
• The pseudocode is:
DijkstraAlgorithm (weighted connected undirected graph) tree = null;
edges = unsorted list of all edges of graph; for each uv in edges
add uv to spanning tree if there is a cycle in the spanning tree then
remove an edge with maximum weight to break the cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Assuming that the edges are ordered alphabetically and not by weight
• We pick the first edge which is ab
• We add ab first
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick ac
• We add ac and check for cycle
• No cycle is detected
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick bc
• We add bc and check for cycle
• Since we have cycle we remove the edge with maximum weight which is bc to break the cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick be
• We add be and check for cycle
• No cycle is detected
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick dc
• We add dc and check for cycle
• No cycle is detected
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick cf
• We add cf and check for cycle
• No cycle is detected
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick de
• We add de and check for cycle
• Since we have cycle we remove the edge with maximum weight which is dc to break the cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick df
• We add df and check for cycle
• Since we have cycle we remove the edge with maximum weight which is de to break the cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick eg
• We add eg and check for cycle
• No cycle is detected
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• Next, we pick fg
• We add fg and check for cycle
• Since we have cycle we remove the edge with maximum weight which is be to break the cycle
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
ab, ac, bc, be, cd, cf, de, df, eg, fg
b
c
a
e
d
g
f
5 9
16
6
15
8
127
3
13
• There is no more edge to add.
• All vertices are connected to each other
• We are done
• What is the running time?
b
c
a
e
d
g
f
5
6
8
127
3