Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Unit-4

A graph is an abstract notation used to represent the connection between pairs of


objects. A graph consists of −
 Vertices − Interconnected objects in a graph are called vertices. Vertices are also known as nodes.

 Edges − Edges are the links that connect the vertices.

There are two types of graphs −


 Directed graph − In a directed graph, edges have direction, i.e., edges go from one vertex to another.

 Undirected graph − In an undirected graph, edges have no direction.

Graph Coloring

Graph coloring is a method to assign colors to the vertices of a graph so that no two
adjacent vertices have the same color. Some graph coloring problems are −
 Vertex coloring − A way of coloring the vertices of a graph so that no two adjacent vertices share the same color.

 Edge Coloring − It is the method of assigning a color to each edge so that no two adjacent edges have the same color.

 Face coloring − It assigns a color to each face or region of a planar graph so that no two faces that share a common boundary have the same color.

Chromatic Number

Chromatic number is the minimum number of colors required to color a graph. For
example, the chromatic number of the following graph is 3.

The concept of graph coloring is applied in preparing timetables, mobile radio


frequency assignment, Suduku, register allocation, and coloring of maps.
Steps for graph coloring
 Set the initial value of each processor in the n-dimensional array to 1.

 Now to assign a particular color to a vertex, determine whether that color is already assigned to the adjacent vertices or not.
 If a processor detects same color in the adjacent vertices, it sets its value in the array to 0.

 After making n2 comparisons, if any element of the array is 1, then it is a valid coloring.

G = ( V, E ) where V is vertices and E is edges

Two common graph representations:

1. Adjacency list. Good for sparse graphs and is generally preferred.


2. Adjacency matrix. Only reasonable when a graph is sparse.

A dense graph is when | E | ~ |V|2 , a  sparse graph has | E | << | V |2

Adjacency-List Representation:

Adjacency is an array with | V | lists, one for each vertex. u  V Adj[u] is the
adjacency list for vertex u and points to all vertices v so that the edge ( u,
v ) E. If G is a directed graph, then:

 | Adj [u] | = | E |
u  V 
If G is an undirected graph, then:

 | Adj [u] | = 2 | E |  
u  V 
 

With the adjacency-list representation we have that:

 O ( max ( | V |, | E | )) = O ( | V | + | E | )

memory is required. This is as small as we can imagine if G does not have a


special (computable) structure. If G is a weighted graph then for each f E
we associate wf. with the adjacency-list representation we merely store wf in
the Adj[u] corresponding to v where ( u, v ) = f, the edge in question. Many
other graph variants are easily incorporated into the adjacency-lists
representation.
Minimal Spanning Tree
A spanning tree whose sum of weight (or length) of all its edges is less than all other
possible spanning tree of graph G is known as a minimal spanning tree or minimum
cost spanning tree. The following figure shows a weighted connected graph.

Some possible spanning trees of the above graph are shown below −
Among all the above spanning trees, figure (d) is the minimum spanning tree. The
concept of minimum cost spanning tree is applied in travelling salesman problem,
designing electronic circuits, Designing efficient networks, and designing efficient
routing algorithms.
To implement the minimum cost-spanning tree, the following two methods are used −
 Prim’s Algorithm
 Kruskal’s Algorithm

Prim's Algorithm

Prim’s algorithm is a greedy algorithm, which helps us find the minimum spanning tree
for a weighted undirected graph. It selects a vertex first and finds an edge with the
lowest weight incident on that vertex.
Steps of Prim’s Algorithm
 Select any vertex, say v1 of Graph G.

 Select an edge, say e1 of G such that e1 = v1 v2 and v1 ≠ v2 and e1 has minimum weight among the edges incident on v1 in graph G.

 Now, following step 2, select the minimum weighted edge incident on v2.

 Continue this till n–1 edges have been chosen. Here n is the number of vertices.

The minimum spanning tree is −


Kruskal's Algorithm

Kruskal’s algorithm is a greedy algorithm, which helps us find the minimum spanning
tree for a connected weighted graph, adding increasing cost arcs at each step. It is a
minimum-spanning-tree algorithm that finds an edge of the least possible weight that
connects any two trees in the forest.
Steps of Kruskal’s Algorithm
 Select an edge of minimum weight; say e1 of Graph G and e1 is not a loop.

 Select the next minimum weighted edge connected to e1.

 Continue this till n–1 edges have been chosen. Here n is the number of vertices.
The minimum spanning tree of the above graph is −

Shortest Path Algorithm


Shortest Path algorithm is a method of finding the least cost path from the source
node(S) to the destination node (D). Here, we will discuss Moore’s algorithm, also
known as Breadth First Search Algorithm.
Moore’s algorithm
 Label the source vertex, S and label it i and set i=0.

 Find all unlabeled vertices adjacent to the vertex labeled i. If no vertices are connected to the vertex, S, then vertex, D, is not connected to S. If there
are vertices connected to S, label them i+1.

 If D is labeled, then go to step 4, else go to step 2 to increase i=i+1.

 Stop after the length of the shortest path is found.

Single Source Shortest Paths

Introduction:
In a shortest- paths problem, we are given a weighted, directed graphs G = (V, E), with weight function w: E → R mapping edges to real-valued
weights. The weight of path p = (v0,v1,..... vk) is the total of the weights of its constituent edges:

We define the shortest - path weight from u to v by δ(u,v) = min (w (p): u→v), if there is a path from u to v, and δ(u,v)= ∞, otherwise.

The shortest path from vertex s to vertex t is then defined as any path p with weight w (p) = δ(s,t).

The breadth-first- search algorithm is the shortest path algorithm that works on unweighted graphs, that is, graphs in which each edge can be
considered to have unit weight.

In a Single Source Shortest Paths Problem, we are given a Graph G = (V, E), we want to find the shortest path from a given source vertex s ∈ V to
every vertex v ∈ V.

Variants:
There are some variants of the shortest path problem.

o Single- destination shortest - paths problem: Find the shortest path to a given destination vertex t from every vertex v. By shift the
direction of each edge in the graph, we can shorten this problem to a single - source problem.
o Single - pair shortest - path problem: Find the shortest path from u to v for given vertices u and v. If we determine the single - source
problem with source vertex u, we clarify this problem also. Furthermore, no algorithms for this problem are known that run asymptotically
faster than the best single - source algorithms in the worst case.
o All - pairs shortest - paths problem: Find the shortest path from u to v for every pair of vertices u and v. Running a single - source
algorithm once from each vertex can clarify this problem; but it can generally be solved faster, and its structure is of interest in the own right.

Shortest Path: Existence:


If some path from s to v contains a negative cost cycle then, there does not exist the shortest path. Otherwise, there exists a shortest s - v that is simple.
All Pairs shortest path

1. In all pair shortest path, when a weighted graph is represented by its weight matrix W then objective is to find the distance
between every pair of nodes.
2. We will apply dynamic programming to solve the all pairs shortest path.
3. In all pair shortest path algorithm, we first decomposed the given problem into sub problems.
4. In this principle of optimally is used for solving the problem.
5. It means any sub path of shortest path is a shortest path between the end nodes.

Steps:
i. Let Aki,jAi,jk be the length of shortest path from node i to node j such that the label for every intermediate node will be ≤ k.
ii. Now, divide the path from i node to j node for every intermediate node, say ‘k’ then there arises two case.
a. Path going from i to j via k. b. Path which is not going via k.
iii. Select only shortest path from two cases.
iv. Using recursive method we compute shortest path.
v. Initially: A0=W[i,j]A0=W[i,j]
vi. Next computations: Aki,j=minAk−1i,j,Ak−1i,k,Ak−1k,jAi,jk=minAi,jk−1,Ai,kk−1,Ak,jk−1

Algorithm:
Analysis of Algorithm:
i. The first double for loop takes O (n2) time.
ii. The nested three for loop takes O (n3) time.
iii. Thus, the whole algorithm takes O (n3) time.
Example: Compute all pair shortest path for following figure 7.
Solution:

Thus the shortest distances between all pair are obtained.

Or

The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to
find all pair shortest path problem from a given weighted graph. As a result of this
algorithm, it will generate a matrix, which will represent the minimum distance from any
node to all other nodes in the graph.

At first the output matrix is same as given cost matrix of the graph. After that the output
matrix will be updated with all vertices k as the intermediate vertex.
The time complexity of this algorithm is O(V3), here V is the number of vertices in the
graph.
Input − The cost matrix of the graph.
0 3 6 ∞ ∞ ∞ ∞
3 0 2 1 ∞ ∞ ∞
6 2 0 1 4 2 ∞
∞ 1 1 0 2 ∞ 4
∞ ∞ 4 2 0 2 1
∞ ∞ 2 ∞ 2 0 1
∞ ∞ ∞ 4 1 1 0

Output − Matrix of all pair shortest path.


0 3 4 5 6 7 7
3 0 2 1 3 4 4
4 2 0 1 3 2 3
5 1 1 0 2 3 3
6 3 3 2 0 2 1
7 4 2 3 2 0 1
7 4 3 3 1 1 0

Algorithm
floydWarshal(cost)

Input − The cost matrix of given Graph.


Output − Matrix to for shortest path between any vertex to any vertex.
Begin
   for k := 0 to n, do
      for i := 0 to n, do
         for j := 0 to n, do
            if cost[i,k] + cost[k,j] < cost[i,j], then
               cost[i,j] := cost[i,k] + cost[k,j]
            done
         done
      done
      display the current cost matrix
End

Max Flow Problem Introduction


Maximum flow problems involve finding a feasible flow through a single-source, single-sink flow network that is maximum.
Let’s take an image to explain how the above definition wants to say.
Each edge is labeled with capacity, the maximum amount of stuff that it can carry. The goal is to figure out how much stuff can be pushed
from the vertex s(source) to the vertex t(sink).

.
maximum flow possible is : 23

Following are different approaches to solve the problem :


 
1. Naive Greedy Algorithm Approach (May not produce an optimal or correct result)
Greedy approach to the maximum flow problem is to start with the all-zero flow and greedily produce flows with ever-higher value. The
natural way to proceed from one to the next is to send more flow on some path from s to t
How Greedy approach work to find the maximum flow :
E number of edge
f(e) flow of edge
C(e) capacity of edge

1) Initialize : max_flow = 0
f(e) = 0 for every edge 'e' in E

2) Repeat search for an s-t path P while it exists.


a) Find if there is a path from s to t using BFS
or DFS. A path exists if f(e) < C(e) for
every edge e on the path.
b) If no path found, return max_flow.
c) Else find minimum edge value for path P

// Our flow is limited by least remaining


// capacity edge on path P.
(i) flow = min(C(e)- f(e)) for path P ]
max_flow += flow
(ii) For all edge e of path increment flow
f(e) += flow

3) Return max_flow
Note that the path search just needs to determine whether or not there is an s-t path in the subgraph of edges e with f(e) < C(e). This is
easily done in linear time using BFS or DFS.
There is a path from source (s) to sink(t) [ s -> 1 -> 2 -> t] with maximum flow 3 unit ( path show in blue color )

After removing all useless edge from graph it’s look like
For above graph there is no path from source to sink so maximum flow : 3 unit But maximum flow is 5 unit. to over come form this issue we
use residual Graph.
 
2. Residual Graphs
The idea is to extend the naive greedy algorithm by allowing “undo” operations. For example, from the point where this algorithm gets stuck
in above image, we’d like to route two more units of flow along the edge (s, 2), then backward along the edge (1, 2), undoing 2 of the 3 units
we routed the previous iteration, and finally along the edge (1,t)

backward edge : ( f(e) ) and forward edge : ( C(e) – f(e) )

You might also like