7.1 Single-source shortest path:
Graphs can be
used to represent the highway structure of a state or country with
vertices representing cities and edges representing sections of
highway. The edges can then be assigned weights which may be either the
distance between the two cities connected by the edge or the average
time to drive along that section of highway. A motorist wishing to
drive from city A to B would be interested in answers to the following
questions:
- Is there a path from A to B?
- If there is more than one path from A to B? Which is the shortest path?
The
problems defined by these questions are special case of the path
problem we study in this section. The length of a path is now defined
to be the sum of the weights of the edges on that path. The starting
vertex of the path is referred to as the source and the last vertex the
destination. The graphs are digraphs representing streets. Consider a
digraph G=(V,E), with the distance to be traveled as weights on the
edges. The problem is to determine the shortest path from v0 to all the
remaining vertices of G. It is assumed that all the weights associated
with the edges are positive. The shortest path between v0 and some
other node v is an ordering among a subset of the edges. Hence this
problem fits the ordering paradigm.
Example:
Consider the digraph of fig 7-1. Let the
numbers on the edges be the costs of travelling along that route. If a
person is interested travel from v1 to v2, then he encounters many
paths. Some of them are
- v1à
v2 = 50 units
- v1à
v3à
v4à
v2 = 10+15+20=45 units
- v1à
v5à
v4à
v2 = 45+30+20= 95 units
- v1à
v3à
v4à
v5à
v4à
v2 = 10+15+35+30+20=110 units
The cheapest path among these is the path along v1à
v3à
v4à
v2. The cost of the path is 10+15+20 = 45 units. Even though there are
three edges on this path, it is cheaper than travelling along the path
connecting v1 and v2 directly i.e., the path v1à
v2 that costs 50 units. One can also notice that, it is not possible to travel to v6 from any other node.
To formulate a greedy based algorithm to
generate the cheapest paths, we must conceive a multistage solution to
the problem and also of an optimization measure. One possibility is to
build the shortest paths one by one. As an optimization measure we can
use the sum of the lengths of all paths so far generated. For this
measure to be minimized, each individual path must be of minimum
length. If we have already constructed i shortest paths, then using
this optimization measure, the next path to be constructed should be
the next shortest minimum length path. The greedy way to generate these
paths in non-decreasing order of path length. First, a shortest path to
the nearest vertex is generated. Then a shortest path to the second
nearest vertex is generated, and so on.
A much simpler method would be to solve it using matrix representation. The steps that should be followed is as follows,
Step 1: find the adjacency matrix for the given graph. The adjacency matrix for fig 7.1 is given below
|
V1 |
V2 |
V3 |
V4 |
V5 |
V6
|
V1 |
- |
50 |
10 |
Inf |
45 |
Inf |
V2 |
Inf |
- |
15 |
Inf |
10 |
Inf |
V3 |
20 |
Inf |
- |
15 |
inf |
Inf |
V4 |
Inf |
20 |
Inf |
- |
35 |
Inf |
V5 |
Inf |
Inf |
Inf |
30 |
- |
Inf |
V6 |
Inf |
Inf |
Inf |
3 |
Inf |
- |
Step 2: consider v1 to be the source and
choose the minimum entry in the row v1. In the above table the minimum
in row v1 is 10.
Step 3: find out the column in which the
minimum is present, for the above example it is column v3. Hence, this
is the node that has to be next visited.
Step 4: compute a matrix by eliminating
v1 and v3 columns. Initially retain only row v1. The second row is
computed by adding 10 to all values of row v3.
The resulting matrix is
|
V2 |
V4 |
V5 |
V6 |
V1à
Vw |
50 |
Inf |
45 |
Inf |
V1à
V3à
Vw |
10+inf |
10+15 |
10+inf |
10+inf |
Minimum |
50 |
25 |
45 |
inf |
Step 5: find the minimum in each column.
Now select the minimum from the resulting row. In the above example the
minimum is 25. Repeat step 3 followed by step 4 till all vertices are
covered or single column is left.
The solution for the fig 7.1 can be continued as follows
|
V2 |
V5 |
V6 |
V1à
Vw |
50 |
45 |
Inf |
V1à
V3à
V4à
Vw |
25+20 |
25+35 |
25+inf |
Minimum |
45 |
45 |
inf |
|
V5 |
V6 |
V1à
Vw |
45 |
Inf |
V1à
V3à
V4à
V2à
Vw |
45+10 |
45+inf |
Minimum |
45 |
inf |
|
V6 |
V1à
Vw |
Inf |
V1à
V3à
V4à
V2à
V5à
Vw |
45+inf |
Minimum |
inf |
Finally the cheapest path from v1 to all other vertices is given by V1à
V3à
V4à
V2à
V5.
![](http://onestopgate.com/images/Image22.gif)
7.2 Minimum-Cost spanning trees
Let G=(V,E) be an undirected connected graph. A sub-graph t = (V,E1) of G is a spanning tree of G if and only if t is a tree.
Above figure shows the complete graph on four nodes together with three of its spanning tree.
Spanning trees have many applications.
For example, they can be used to obtain an independent set of circuit
equations for an electric network. First, a spanning tree for the
electric network is obtained. Let B be the set of network edges not in
the spanning tree. Adding an edge from B to the spanning tree creates a
cycle. Kirchoff’s second law is used on each cycle to obtain a circuit
equation.
Another application of spanning trees
arises from the property that a spanning tree is a minimal sub-graph G’
of G such that V(G’) = V(G) and G’ is connected. A minimal sub-graph
with n vertices must have at least n-1 edges and all connected graphs
with n-1 edges are trees. If the nodes of G represent cities and the
edges represent possible communication links connecting two cities,
then the minimum number of links needed to connect the n cities is n-1.
the spanning trees of G represent all feasible choice.
In practical situations, the edges have
weights assigned to them. Thse weights may represent the cost of
construction, the length of the link, and so on. Given such a weighted
graph, one would then wish to select cities to have minimum total cost
or minimum total length. In either case the links selected have to form
a tree. If this is not so, then the selection of links contains a
cycle. Removal of any one of the links on this cycle results in a link
selection of less const connecting all cities. We are therefore
interested in finding a spanning tree of G. with minimum cost since the
identification of a minimum-cost spanning tree involves the selection
of a subset of the edges, this problem fits the subset paradigm.
7.2.1 Prim’s Algorithm
A greedy method to obtain a minimum-cost
spanning tree builds this tree edge by edge. The next edge to include
is chosen according to some optimization criterion. The simplest such
criterion is to choose an edge that results in a minimum increase in
the sum of the costs of the edges so far included. There are two
possible ways to interpret this criterion. In the first, the set of
edges so far selected form a tree. Thus, if A is the set of edges
selected so far, then A forms a tree. The next edge(u,v) to be included
in A is a minimum-cost edge not in A with the property that A U {(u,v)}
is also a tree. The corresponding algorithm is known as prim’s
algorithm.
For Prim’s algorithm draw n isolated
vertices and label them v1, v2, v3,…vn. Tabulate the given weights of
the edges of g in an n by n table. Set the non existent edges as very
large. Start from vertex v1 and connect it to its nearest neighbor
(i.e., to the vertex, which has the smallest entry in row1 of table)
say Vk. Now consider v1 and vk as one subgraph and connect this
subgraph to its closest neighbor. Let this new vertex be vi. Next
regard the tree with v1 vk and vi as one subgraph and continue the
process until all n vertices have been connected by n-1 edges.
Consider the graph shown in fig 7.3. There are 6 vertices and 12 edges. The weights are tabulated in table given below.
|
V1 |
V2 |
V3 |
V4 |
V5 |
V6 |
V1 |
- |
10 |
16 |
11 |
10 |
17 |
V2 |
10 |
- |
9.5 |
Inf |
Inf |
19.5 |
V3 |
16 |
9.5 |
- |
7 |
Inf |
12 |
V4 |
11 |
Inf |
7 |
- |
8 |
7 |
V5 |
10 |
Inf |
Inf |
8 |
- |
9 |
V6 |
17 |
19.5 |
12 |
7 |
9 |
- |
Start with v1 and pick the smallest
entry in row1, which is either (v1,v2) or (v1,v5). Let us pick (v1,
v5). The closest neighbor of the subgraph (v1,v5) is v4 as it is the
smallest in the rows v1 and v5. The three remaining edges selected
following the above procedure turn out to be (v4,v6) (v4,v3) and (v3,
v2) in that sequence. The resulting shortest spanning tree is shown in
fig 7.4. The weight of this tree is 41.5.
7.2.3 Kruskal’s Algorithm:
There is a second possible
interpretation of the optimization criteria mentioned earlier in which
the edges of the graph are considered in non-decreasing order of cost.
This interpretation is that the set t of edges so far selected for the
spanning tree be such that it is possible to complete t into a tree.
Thus t may not be a tree at all stages in the algorithm. In fact, it
will generally only be a forest since the set of edges t can be
completed into a tree if and only if there are no cycles in t. this
method is due to kruskal.
The Kruskal algorithm can be illustrated
as folows, list out all edges of graph G in order of non-decreasing
weight. Next select a smallest edge that makes no circuit with
previously selected edges. Continue this process until (n-1) edges have
been selected and these edges will constitute the desired shortest
spanning tree.
For fig 7.3 kruskal solution is as follows,
V1 to v2 =10
V1 to v3 = 16
V1 to v4 = 11
V1 to v5 = 10
V1 to v6 = 17
V2 to v3 = 9.5
V2 to v6 = 19.5
V3 to v4 = 7
V3 to v6 =12
V4 to v5 = 8
V4 to v6 = 7
V5 to v6 = 9
The above path in ascending order is
V3 to v4 = 7
V4 to v6 = 7
V4 to v5 = 8
V5 to v6 = 9
V2 to v3 = 9.5
V1 to v5 = 10
V1 to v2 =10
V1 to v4 = 11
V3 to v6 =12
V1 to v3 = 16
V1 to v6 = 17
V2 to v6 = 19.5
Select the minimum, i.e., v3 to v4
connect them, now select v4 to v6 and then v4 to v5, now if we select
v5 to v6 then it forms a circuit so drop it and go for the next.
Connect v2 and v3 and finally connect v1 and v5. Thus, we have a
minimum spanning tree, which is similar to the figure 7.4.
7.3 Techniques for graphs:
A fundamental
problem concerning graphs is the reachability problem. In its simplest
form it requires us to determine whether there exists a path in the
given graph G=(V,E) such that this path starts at vertex v and ends at
vertex u. A more general form is to determine for a given starting
Vertex v belonging to V all vertices u such that there is a path from v
to u. This latter problem can be solved by starting at vertex v and
systematically searching the graph G for vertices that can be reached
from v. The 2 search methods for this are :
- Breadth first search.
- Depth first search.
7.3.1 Breadth first search:
In Breadth first search we start at vertex v and mark it as having been
reached. The vertex v at this time is said to be unexplored. A vertex
is said to have been explored by an algorithm when the algorithm has
visited all vertices adjacent from it. All unvisited vertices adjacent
from v are visited next. There are new unexplored vertices. Vertex v
has now been explored. The newly visited vertices have not been
explored and are put onto the end of the list of unexplored vertices.
The first vertex on this list is the next to be explored. Exploration
continues until no unexplored vertex is left. The list of unexplored
vertices acts as a queue and can be represented using any of the
standard queue representations.
7.3.2 Depth first search:
A depth first search of a graph differs from a breadth first search in
that the exploration of a vertex v is suspended as soon as a new vertex
is reached. At this time the exploration of the new vertex u begins.
When this new vertex has been explored, the exploration of u continues.
The search terminates when all reached vertices have been fully
explored. This search process is best-described recursively.
Algorithm DFS(v)
{
visited[v]=1
for each vertex w adjacent from v do
{
If (visited[w]=0)then
DFS(w);
}
}
Post Resume: Click here to Upload your Resume & Apply for Jobs |