0% found this document useful (0 votes)
7 views

Lecture No. 7 Graphs Algorithms

The document covers various graph algorithms, including terminology, representations (matrix and linked list), traversal methods (BFS and DFS), spanning trees, and shortest path algorithms like Dijkstra's. It explains different types of graphs such as undirected, directed, weighted, and special graphs like cliques and bipartite graphs. Additionally, it details the BFS algorithm, its applications, and provides pseudo code for implementation.

Uploaded by

nzangi824
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture No. 7 Graphs Algorithms

The document covers various graph algorithms, including terminology, representations (matrix and linked list), traversal methods (BFS and DFS), spanning trees, and shortest path algorithms like Dijkstra's. It explains different types of graphs such as undirected, directed, weighted, and special graphs like cliques and bipartite graphs. Additionally, it details the BFS algorithm, its applications, and provides pseudo code for implementation.

Uploaded by

nzangi824
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 134

Graph Algorithms

Graph Algorithms / Dr.A.Sattar /1


Graph Algorithms
Topics
ƒ Graph terminology

ƒ Graph Representations
-Matrix Representation
-Linked list Representation

ƒGraph Traversal
-Breath First Search (BFS)
-BFS Analysis
-Depth First Search (DFS)
-DFS Analysis

ƒ Spanning Trees
- Kruskal’s algorithm and analysis
- Prim’s algorithm

ƒ Single Source shortest distance


- Dijkstra’s algorithm

Graph Algorithms / Dr.A.Sattar /2


Graphs
Terminology
A graph G =(V,E) is a set of vertices V and set of edges E, where E ⊆ V x V.
ƒAn example of a graph with vertices v1, v2, v3, v4 , v5 is

ƒThe number of vertices and edges in the graph is given by the set cardinalities of the sets
V, G . In the example, |V| = 5 ,|E| =25

Graph Algorithms / Dr.A.Sattar /3


Graphs
Undirected
ƒ A graph G=(V,E) with vertex set V={v1, v2, v3,……vn } is called undirected if
(vi, vj) = ( vj, vi) for i ≠ j
ƒ An undirected graph is sometimes referred to as undigraph.
ƒ In pictorial representation of undirected graph, the edges are not assigned any direction.
Figure shows an example of undirected graph.

ƒA graph which has all the edges is referred to as complete. An undirected complete graph ,
with n vertices, has n(n-1)/2 edges. Its space complexity is O(n2) . Such a graph is called
dense. By contrast, a graph with space complexity O(n lg n) is called sparse.

Graph Algorithms / Dr.A.Sattar /4


Graphs
Directed
ƒ A graph G=(V,E) with vertex set V={v1, v2 ,v3,……vn } is called directed or digraph if
(vi, vj) ≠ ( vj, vi) for i ≠ j
In other words edges (vi, vj) and (vj, vi), associated with any pair of vertices vi, vj , are
considered distinct. In pictorial representation these are shown with arrows.

ƒThe directed graph is often referred to digraph or network. Figure shows an example of
directed graph

ƒA complete directed graph with n vertices has n2 edges, including edges associated with
all individual vertices. If individual edges are excluded, the total number of edges is n(n-1)

Graph Algorithms / Dr.A.Sattar /5


Graphs
Weighted
ƒ A graph in which values w1, w2, w3,……are associated with edges is called weighted
graph. Weights are typically costs or distances in different applications of graphs.
ƒA weighted graph can be directed or undirected. Figures show examples of such graphs.

ƒWeighted graphs have several real life applications, such as air routes, computer
networks, road links

Graph Algorithms / Dr.A.Sattar /6


Graphs
Paths
ƒ A path is a sequence of vertices which are connected by the consecutive edges.
The number of edges is called the path length. For example, in the figure the path { v1,v2,v4,v6}
has length 3

(a)Path length of 2

ƒ In simple path no vertices are repeated. The path { v1,v2,v4,v6} is simple.


The path {v1,v3, v4, v7, v3,v6} is non-simple, because it passes through vertex v3 twice

(b) Non-simple path

Graph Algorithms / Dr.A.Sattar /7


Graphs
Loops and Cycles
ƒA loop is special path that originates and terminates at a single node. In the sample graph
a loop is associated with vertex v1..

ƒA path that originates and terminates at the same vertex, and links two or more vertices,
is called cycle. In the picture of the cyclic graph the path v2,v3, v4, v2 is cycle.
If a graph contains a cycle it is called cyclic. By contrast, a graph which contains no
cycles is known as acyclic.

loop

cycle

(a) cyclic graph (b) acyclic graph

Graph Algorithms / Dr.A.Sattar /8


Special Graphs
Free Tree
ƒA free tree is a connected graph such that there is unique simple path between any
pair of vertices
ƒ A free tee is acyclic , that is, it has no cycles. A free tree has no unique vertex that can be
designated as .root.
ƒ Figure shows an example of a special connected graph which is a free tree.

v3 v5 v7 v10 v15

v1 v2 v6 v8 v11 v12 v14 v16

v4 v9 v13

Example of a free tree

ƒ Free trees are useful in constructing optimal networks

Graph Algorithms / Dr.A.Sattar /9


Special Graphs
Cliques
ƒ A clique is a complete subgraph in an undirected graph. In other words, every vertex in the
subgraph is connected to all other vertices. A graph can none, one or more cliques
ƒ Figure (a) shows an undirected graph. Figure (b) shows cliques of sizes 3 and 4 in the graph.
The edges of cliques are depicted in red color .
v2 v4
v2 v4

v1 v3 v5 v6
v1 v3 v5 v6

v9 v8 v7
v9 v8 v7

(a) Undirected graph (b) Cliques: {v1,v2,v3,v8,v9}, {v4,v5,v6 ,7}

ƒ The cliques represent robust subnets in a network. If a path in a clique is disrupted, the
network continues to function.

ƒ A fundamental problem in computer science is to find clique of maximum size in a large


Graph. No efficient algorithm is known for the solution of this problem.

Graph Algorithms / Dr.A.Sattar /10


Special Graphs
Bipartite Graphs
ƒ A bipartite graph is an undirected graph, whose vertices can be partitioned into two distinct sets V1 and
V2, such that each vertex in set V1 is linked to one or more vertices in set V2, but the vertices in each set are
not linked.
ƒ The vertices in each set can be assigned different colors. Since adjacent vertices have different colors, the
bipartite graph is said to be 2-colorable
v1 v1 v2
w1

v2
V1 w2 V2
v3
v4 v3
w3
v4
(b) Non-Bipartite graph
(a) Bipartite graph
ƒ The Bipartite graphs are useful in optimal matching and resource allocation problems, for example,
assigning courses to a set of instructors so that maximum number of courses can be offered.
ƒFigures (c) and (d) show a graph which represents instructors and courses. Assuming an instructor can take
one or more courses but is allowed to teach one course. Figure (c) shows a possible offering of
two courses. The figure (d) depicts the optimum offering of 3 courses.
I1 I1
C1 C1

I2 I2
C2 C2

I3 I3
C3 C3

I4 I4
C4 C4

I5 I5
(c) Offering of two courses (d) Offering of three courses
Graph Algorithms / Dr.A.Sattar /11
Special Graphs
Articulation Vertex
ƒAn articulation vertex in connected graph is a special vertex whose deletion disconnects
one or more components of graph.
ƒFigure shows a connected graph with an articulation vertex( colored red).

v3 v5 v7 v10 v15

v1 v2 v6 v8 v11 v12 v14 v16

v4 v9 v13

Connected graph with articulation vertex v11

ƒArticulation vertices play a crucial role in network security. Efficient algorithms are available
to find such vertices in a large network.

Graph Algorithms / Dr.A.Sattar /12


Graph Representation

Graph Algorithms / Dr.A.Sattar /13


Graphs
Matrix Representation
ƒ One of the standard ways was of representing graph is to use a matrix to denote links
between pairs of vertices in a graph. The matrix is known as adjacency matrix.

Let G=(V,E) be a graph, with V={v1,v2,……vn}, and E={(v1,v2),(v1,v3)….}


where n=|V|. The adjacency matrix A=(aij) as

1 if (vi, vj) ∈E
aij =
0 otherwise

ƒ The definition implies that entry in the ith row and jth column of the matrix is 1
if a link exists between the ith and jth vertex; otherwise, it is 0.

ƒ The size |V|x|V| of adjacency matrix is dependent on the number of vertices. The
memory requirement is θ(|V|2)

Graph Algorithms / Dr.A.Sattar /14


Graphs
Matrix Representation
ƒFigures (a)-(c) show a graph, the vertices and edges sets, and matrix representation

(a) Directed graph

(c) Adjacency Matrix

(b) Vertices and edges sets

Graph Algorithms / Dr.A.Sattar /15


Graphs
Linked List Representation
ƒ A graph can also be represented using linked lists. The list representation consists of
an array of linked lists, each corresponding to a vertex. The list stores all of the vertices
that are linked to a given vertex.

ƒLet G=(V,E) be a graph, and Adj(u) be the linked list corresponding to vertex u, then
for all v,
v ∈ Adj(u), if (u ,v )∈ E.

ƒThe vertices belonging to Adj(u) are called u’s immediate neighbors, or adjacent vertices.

Graph Algorithms / Dr.A.Sattar /16


Graphs
Linked List Representation
Figure (a)-(c) show a graph, the vertices and edges sets, and linked list representation

(a) Directed graph

(c) Adjacency List

(b) Vertices and edges sets


Graph Algorithms / Dr.A.Sattar /17
Graph Traversal

Graph Algorithms / Dr.A.Sattar /18


Breadth First Search
Strategy
ƒ The Breadth First Search (BFS) is one of the standard graph traversal methods. Each node
is visited once only. The method proceeds by visiting all immediate neighbors
( vertices one edge away), then visiting neighbor’s neighbors (vertices two edges away),
and so forth, until all reachable vertices have been visited.

ƒ If a graph has any subgraph (which is not reachable from the initial vertex),
the procedure may be re-started by choosing a vertex from the unvisited subgraph.

ƒ A queue is used to implement the BFS procedure. It keeps track of vertices


to be visited next. A vertex pushed to the queue is said to be in ready state.
An enqueued vertex is said be in wait state. The dequeued vertex is referred to as processed.

ƒ The queue is initialized with the address of starting vertex.

Graph Algorithms / Dr.A.Sattar /19


Breadth First Search
Algorithm
ƒ The Breadth First Search (BFS) for a connected graph consists of following steps:

Step #1: Initialize all vertices to mark as unvisited

Step #2: Select an arbitrary vertex, add it to queue

Step #3: Repeat steps # 4 through Step #5 until queue is empty

Step #4: Remove an element from the queue. Process and mark it as visited

Step #5: Enqueue all adjacent vertices of the processed vertex. Go to Step # 3.

ƒ The same algorithm may be applied to a subgraph, which contains any unvisited vertices.

Graph Algorithms / Dr.A.Sattar /20


Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /21


Cont’d-1
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /22


Cont’d-2
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /23


Cont’d-3
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /24


Cont’d-4
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /25


Cont’d-5
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /26


Cont’d-6
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /27


Cont’d-7
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /28


Cont’d-8
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /29


Cont’d-9
Breadth First Search
Example

skipping intermediate steps

Graph Algorithms / Dr.A.Sattar /30


Cont’d-10
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /31


Cont’d-11
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /32


Cont’d-12
Breadth First Search
Example

Graph Algorithms / Dr.A.Sattar /33


Breadth First Search
Applications
ƒ Apart from gleaning information stored in vertices, the BFS has the following useful
applications:
ƒThe edges between consecutively visited vertices form a free tree called BFS spanning tree, as
shown in the figure

BFS Spanning Tree Breadth First Search

ƒThe BFS can find the number of disconnected subgraphs of a graph

ƒThe BFS can be used to find articulation vertices in a graph

Graph Algorithms / Dr.A.Sattar /34


Breadth First Search
Pseudo Code
The BFS implements breadth-first-search procedure by using a queue Q. The address s of first
selected vertex is passed to the method. The implementation uses three color codes to keep
track of the state of vertices: GREEN for unvisited vertex, YELLOW for vertex in wait state,
PINK for processed vertex. The graph is represented by adjacency list Adj
BFS(G, s) ► s is address of starting vertex
1 for each vertex u ∈
V[G] – { s } ►V[G] is vertex set of graph G. V[G]-{s} is the
difference set that excludes initial vertex s in {s}.
2 do color[u] ← GREEN ►All unvisited vertices are assigned color GREEN
3 color[s] ← YELLOW ► Vertex s is being pushed to queue and assigned color YELLOW
4 Q ←φ ► Queue is initialized as empty
5 ENQUEUE(Q,s) ► The starting vertex s is added to the queue
6 while Q ≠ φ ►A loop is created to remove vertices from the queue.
7 do u←DEQUEUE(Q) ► u is the removed vertex
8 for each v ∈ Adj[u] ► Examine all vertices v that are adjacent to vertex u
9 do if color[v] = GREEN ►If the neighbor vertex v is not visited,
10 then color[v]← YELLOW then assign it color YELLOW( wait state)
11 ENQUEUE(Q,v) and add to the queue
12 color[u] ← PINK ►The vertex u removed from the queue is marked visited
and assigned color PINK

Visualization
Graph Algorithms / Dr.A.Sattar /35
Analysis of Breadth First Search
Worst Case
ƒThe running time for BFS consists of following major components
Ti = Initializing vertices T
Tq = Queuing and Dequeuing
Ts = Scanning Adjacency list

ƒ The Graph G(V,E) has |V| vertices and |E| edges. Each vertex is initialized once. Therefore,
Ti = |V|
ƒ The queuing and dequeuing operations take O(1) time, and each vertex is queued
and dequeued once,
Tq = 2|V|.O(1)
ƒ The scanning of Adjacency list takes place for each vertex. In worst case all links are
are examined. The total time is proportional to the number of edges scanned. Since |E| is
the number of edges in G,
Ts = |E|

ƒThus time running time TBST is given by


TBST= |V|+2|V|.O(1)+ |E|= O(|V|+|E|)

Graph Algorithms/Dr.A.Sattar
Graph Algorithms / Dr.A.Sattar /36
Depth First Search
Strategy
ƒ The Depth First Search (DFS) is one of the standard graph traversal methods. Each node
is visited once only. The method proceeds by first visiting all distant neighbors

ƒ If a graph has any sub-graph (which is not reachable from the initial vertex),
the procedure is re-started by choosing a vertex from the unvisited sub-graph.

ƒ A stack is used to implement the DFS procedure. It keeps track of vertices


to be visited next. A vertex to be pushed to the stack is said to be in ready state.
A pushed vertex is said be in wait state. The popped vertex is referred to as processed.

ƒ The stack is initialized with the address of starting vertex.

Graph Algorithms / Dr.A.Sattar /37


Depth First Search
Algorithm
ƒ The Depth First Search (DFS) for a connected graph consists of following steps:

Step #1: Initialize all vertices to mark as unvisited

Step #2: Select an arbitrary vertex, push it to stack

Step #3: Repeat steps # 4 through Step #5 until stack is empty

Step #4: Pop off an element from the stack. Process and mark it as visited

Step #5: Push to stack adjacent vertices of the processed vertex. Go to Step # 3.

ƒ The same algorithm is applied to a subgraph, which contains any unvisited vertices.

Graph Algorithms / Dr.A.Sattar /38


Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /39


Cont’d-1
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /40


Cont’d-2
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /41


Cont’d-2
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /42


Cont’d-3
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /43


Cont’d-4
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /44


Cont’d-5
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /45


Cont’d-6
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /46


Cont’d-7
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /47


Cont’d-8
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /48


Cont’d-9
Depth First Search
Example

Skipping intermediate steps


Graph Algorithms / Dr.A.Sattar /49
Cont’d-10
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /50


Cont’d-11
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /51


Cont’d-12
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /52


Cont’d-13
Depth First Search
Example

Graph Algorithms / Dr.A.Sattar /53


Depth First Search
Applications
ƒ Apart from extracting information stored in vertices, the DFS has the following applications:
ƒThe edges between consecutively visited vertices form a free tree called DFS spanning tree, as
shown in the figure

DFS Spanning Tree Depth First Search

ƒThe DFS can find shortest distance from the source vertex to a distant vertex

ƒIt can be used to find if a graph has cycles

Graph Algorithms / Dr.A.Sattar /54


Depth First Search
Pseudo Code
The DFS implements depth-first-search procedure by using a stack S. The address s of first selected
vertex is passed to the method. The implementation uses three color codes to keep track of the state of
vertices: GREEN for unvisited vertex, YELLOW for vertex in wait state, PINK for processed vertex.
The graph is represented by adjacency list Adj

DFS(G, s) ► s is address of starting vertex



1 for each vertex u V[G] – { s } ►V[G] is vertex set of graph G. V[G]-{s} is the
difference set that excludes initial vertex s.
2 do color[u] ← GREEN ►All unvisited vertices are assigned color GREEN
3 color[s] ← YELLOW ► Vertex s is being pushed to stack and assigned color YELLOW
4 P ←φ ► Stack is initialized as empty
5 PUSH(S,s) ► The starting vertex s is added to the queue
6 while S ≠ φ ►A loop is created to pop off vertices .
7 do u←POP(S) ► u is the popped vertex
8
9
for each v ∈ Adj[u]► Examine all vertices v that are adjacent to vertex u
do if color[v] = GREEN ►If the neighbor vertex v is not visited,
10 then color[v]← YELLOW then assign it color YELLOW( wait state)
11 PUSH(S,v) and push it on to the stack
12 color[u] ← PINK ►The vertex u popped off the stack is marked visited
and assigned color PINK

Visualization
Graph Algorithms / Dr.A.Sattar /55
Analysis of Depth First Search
Worst Case
The running time for DFS consists of following major components
Ti = Initializing vertices T
Tp = pushing and popping
Ts = Scanning Adjacency list

ƒ The Graph G(V,E) has |V| vertices and |E| edges. Each vertex is initialized once. Therefore,
Ti = |V|

ƒ The pushing and popping operations take O(1) time, and each vertex is pushed
and popped once,
Tp = 2|V|.O(1)

ƒ The scanning of Adjacency list takes place for each vertex. The total time is
proportional to the number of edges scanned. Since |E| is the number of edges in G, in worst
case all edges are examined. Therefore,
Ts = |E|

ƒ Thus, the worst case running time is given by time


TDFS= |V|+2|V|.O(1)+ |E|= O(|V|+|E|)
Graph Algorithms / Dr.A.Sattar /56
Spanning Trees

Graph Algorithms / Dr.A.Sattar /57


Graphs
Spanning Trees
ƒA spanning tree for an undirected graph is a sub-graph which includes all vertices
but has no cycles.

ƒ There can be several spanning trees for a graph. Figure shows some of the trees
for the graph with vertices v1,v2,v3,v4

v1 v2
v1 v2 v1 v2 v1 v2 v1 v2

v4 v3
v4 v3 v4 v3 v4 v3 v4 v3

Graph
Spanning Trees

ƒ Each tree has same number of edges

ƒ Spanning trees can be generated by depth-first–search and breadth-first-search


procedures.

Graph Algorithms / Dr.A.Sattar /58


Minimum Spanning Tree
Definition
ƒ A weighted undirected graph can have several spanning trees.

ƒOne of the spanning trees has smallest sum of all the weights associated with the
edges. This tree is called minimum spanning tree (mst).

ƒ Figure shows a sample weighted graph, some of the spanning trees, and the minimum
spanning tree.

20 20 20 20 20
v1 v2 v1 v2 v1 v2 v1 v2 v1 v2
30 50 40
10 10 30 10 10 40
50
v4 60 v3 v4 v3 v4 60 v3 v4 60 v3 v4 v3

cost=10+20+30=50 cost=10+20+60=90 cost=20+50+60=130 cost= 10+20+40=70

(a) Weighted graph (b) Minimum Spanning (c) Spanning Trees


Tree

Graph Algorithms / Dr.A.Sattar /59


Minimum Spanning Trees
Applications
ƒ Minimum spanning trees have many practical applications. Some typical
applications are:

• A telephone network can be configured, using minimum spanning tree, to have


minimum cable length.

• The air travel routes can be selected so that the travel time or travel cost is least.

• A computer network can be set up with minimum routing distance

• Linking a group of islands with bridges so that total bridge span length is minimum

ƒ Two important algorithms for creating a minimum spanning tree for a graph,
named after their inventors, are Kruskal’s algorithm and Prim’s algorithm.

Graph Algorithms / Dr.A.Sattar /60


Minimum Spanning Tree
Kruskal’s Algorithm
ƒ The Kruskal ‘s algorithm works as follows:

Step #: 1 Remove all edges of the graph


Step #:2 Arrange edges according to their weights
Step # 3: Select a edge with least weight
Step #4: Attach the edge to the corresponding vertices if it does not form a cycle;
otherwise, drop the edge
Step #5: Repeat steps 3 to 4 until all the edges are processed (added or dropped)

ƒ Kruskal’s algorithm is categorized as greedy, because at each step it picks an edge


with least weight.

ƒ The Kruskal’s algorithm can be implemented in several ways. Generally, a priority


queue is used to store graph edges, so that edges are retrieved in increasing order.

Graph Algorithms / Dr.A.Sattar /61


Kruskal’s Algorithm
Example
Kruskal’s algorithm is used to create a Minimum Spanning Tree. A sample graph is used to
demonstrate the algorithm.

Sample weighted graph

Graph Algorithms / Dr.A.Sattar /62


Kruskal’s Algorithm
Example
Edges are stored into a priority queue. The priorities are weights, so that the smallest weight is
extracted first.

Graph Algorithms / Dr.A.Sattar /63


Kruskal’s Algorithm Cont’d-1

Example
Edge EF is extracted. It is added to the spanning tree .

Graph Algorithms / Dr.A.Sattar /64


Cont’d-2
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /65


Cont’d-3
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /66


Cont’d-4
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /67


Cont’d-5
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /68


Cont’d-6
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /69


Cont’d-7
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /70


Cont’d-8
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /71


Cont’d-9
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /72


Kruskal’s Algorithm Cont’d-10

Example

Graph Algorithms / Dr.A.Sattar /73


Cont’d-11
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /74


Cont’d-12
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /75


Cont’d-13
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /76


Cont’d-14
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /77


Cont’d-15
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /78


Cont’d-16
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /79


Cont’d-17
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /80


Cont’d-18
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /81


Cont’d-19
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /82


Cont’d-20
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /83


Cont’d-21
Kruskal’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /84


Cont’d-22
Kruskal’s Algorithm
Example
ƒThe minimum spanning tree is generated The minimum distance is 182.

ƒNote that the minimum spanning tree includes all of the graph vertices.

Graph Algorithms / Dr.A.Sattar /85


Kruskal’s Algorithm
Implementation
ƒ It follows that The Kruskal’s algorithm involves two basic stemps:(1) Sorting edges in non-decreasing
order (2) discarding the edges that form cycles with the already selected edges.

ƒ In actual implementation, the edges can be sorted by using an efficient algorithm such as quick sort.
Alternatively, a Priority Queue, based on min-heap, can be used. The edges are retrieved in
non-decreasing order by dequeue operation, as shown in the preceding example.

ƒThe problem of detection of cycle can be handled by using a Union set. A Union set is a set
of disjoint sets of vertices. For example, if V={a ,b, c, d ,e} is the vertex set, then Union Set
is defined as the set UV={ {a}, {b}, {c}, {c}, {d}, {e} } In order to form the minimum spanning
tree, an edge is extracted from the priority queue. Next, the elements of UV are examined.
If the vertices belong to the same disjoint set, the edge is discarded; otherwise, the set UV
is updated by merging the disjoint sets.

ƒ For example, if at stage the UV consists of elements {a}, {b}, { c, e}, {d}, and edge ed is
extracted, then the Union set would be updated by merging the subsets {c, e} and {d}. In other words,
the updated UV will be to {{a}, {b}, {c, d, e}}. Again, assuming an edge ce is extracted.
Since e and d belong to the same set {c, d, e} the edge will be discarded Again, assume that the edge ab
is extracted. Since the vertices a, b belong to different sets, the corresponding disjoints set, namely,
{a}, {b} will be merged. Thus, the updated UV would be UV={{a, b}, {c, d, e} }. Next , assume
that edge bc is selected, the sets {a, b} and {c, d, e} would be merged i.e UV={a, b, c, d, e}

Graph Algorithms / Dr.A.Sattar /86


Kruskal’s Algorithm
Implementation Using Set Union
90 90
v1 v1
v2 v2
30 30 40 10
20 40 10 20
70 55 70
v6 55 v7 v6 v7 v3
v3
80 50 80
35 50 35 65
65
v5 v5
v4 v4
95

Edge Weight Edge Action UV={{v1}, {v2}, {v3}, {v4}, {v5}, {v6}, {v7} }
(v2,v3) 10 (v2,v3) added { {v1}, {v2, v3}, {v4}, {v5}, {v6}, {v7} }

(v1,v7) 20 (v1,v7) added {{v1,v7}, {v2,v3},{v4}, {v5},{v6}}

(v1,v6) 30 (v1,v6) added {{v1,v6,v7}, {v2,v3},{v4},{v5} }

(v5,v6) 35 (v5,v6) added { {v1, v5,v6,v7}, {v2,v3},{v4} }

(v2,v7) 40 (v2,v7) added { {v1,v2,v3,v5,v6,v7},{v4}

(v5,v7) 50 (v5,v7) rejected

(v6,v7) 55 (v6,v7) rejected

(v3,v4) 65 (v3,v4) added {{v1,v2,v3,v4,v5,v6,v7}}

(v3,v7) 70 (v3,v7) rejected

(v4,v7) 80 (v4,v7) rejected

(v1,v2) 90 (v1,v2) rejected

(v4,v5) 95 (v4,v5) rejected

Graph Algorithms / Dr.A.Sattar /87


Kruskal’s Algorithm
Pseudo Code
Following is pseudo code for Kruskal’s algorithm.

MST-KRUSKAL(G,W)
1 T←φ ►Initialize Minimum Spanning Tree
2 VS←φ ► Initialize Union set VS

3 for each vertex v V
4 do VS ← VS U{v} ► Add Vertex sets to union set
5 for each (u, v) ∈ E ►Create a priority queue of edges and weights
6 do ENQUEUE(Q,u,v,w) ► weights are keys (priorities)
7 while |VS| >1 do ► Continue until all vertices form a single set of cardinality 1
8 (u,v) ←DEQUEUE(Q) ►Remove an edge (u,v) from the queue
9 if u, v belong to different sets VS1∈ VS and VS2 VS ∈
10 then VS ←VS – VS1 ► remove set VS1 from VS
11 VS←VS - VS2►remove set VS2 from VS
12 VS ← VS U ( VS1 U VS2) ►Join sets VS1 and VS2 , add the union to VS
13 T←(u, v) ►Add the selected edge to the spanning tree
14 return T

Visualization

Graph Algorithms / Dr.A.Sattar /88


Analysis of Kruskal’s Algorithm
ƒAssuming that priority queue is implemented as a heap. The time to sort edges
is O(|E|. lg| E|).

ƒThe initialization of VS takes O(|V|).

ƒ The total time to find sets VS1 and VS2 and replacing them by their union
is at most O(|E|)

Thus, on the whole Kruskal’s algorithm runs in time O(|E|. lg| E|) + O(|E|)+O(|V|)
.

Graph Algorithms / Dr.A.Sattar /89


Minimum Spanning Trees
Prim’s Algorithm
ƒThe Prim’s algorithm is an alternative method for creating minimum spanning trees for
a weighted undirected graph. Unlike the Kruskal’s algorithm, the Prim’s algorithm
makes a systematic selection of vertices. It proceeds by choosing a vertex, which has
the shortest distance of all of the linked vertices.

ƒ Let V be the vertex set for a graph G. Let T be the minimum spanning tree for G. The
Prim’s algorithm proceeds as follows:
Step #:1 Select some vertex s in V , as the starting vertex.

Step # 2: Add vertex s to an empty set S. Remove s from V.

Step # 3: Repeat Step #:4 through Step #:6 until the set V is empty.

Step # 4: Examine all vertices in S which are linked to vertices in V.

Step #5 :Choose the vertex u in V which has the minimum distance from vertex v in S.

Step #6: Remove vertex u from V and add it to S. Move edge (v ,u) to T.

Graph Algorithms / Dr.A.Sattar /90


Prim’s Algorithm
Example

A sample weighted graph with vertex set V= {A,B, C, D, E, F, G, H, I, J} is used to


demonstrate Prim’s algorithm for creating a Minimum Spanning Tree. The distances
between the linked vertices are shown next to graph edges.

Graph Algorithms / Dr.A.Sattar /91


Prim’s Algorithm Cont’d-1

Example
Initially, the set V contains all of the graph vertices. Another set G holds the processed
vertices. First, vertex A is chosen. It is deleted from V and placed in set G. A is marked as
selected, and shown in pink color.

Graph Algorithms / Dr.A.Sattar /92


Prim’s Algorithm Cont’d-2

Example
ƒ All vertices in set V, which are linked to the vertices in set S, are examined. The vertex
which has the shortest distance is selected, and placed in set S. The selected vertices form
path of the Minimum Spanning Tree.

ƒVertices B, I, J in set V are linked to vertex A in set S, and have distances 10, 30, 28
respectively. Since B has minimum distance 10, it is selected . The corresponding edge of
the subgraph, or segment of minimum spanning tree, is shown in red color.

Graph Algorithms / Dr.A.Sattar /93


Prim’s Algorithm Cont’d-3

Example
Vertices I, J in set V are linked to vertex A, and have distances 30, 28. Vertices C, J in set V
are linked to vertex B in set S and have the distances 15, 17. Since vertex C has the minimum
distance 15 , it is selected. The vertex C is removed from set V, and placed in set S.

Graph Algorithms / Dr.A.Sattar /94


Prim’s Algorithm Cont’d-4

Example
Vertex I in set V is linked to vertex A, and has distances 30. Vertex J in set V is linked to
vertices A, B and C in set S and has the distances 28, 17,40 . Vertex D in set V is linked to
vertex C in S and has distance 45. Since vertex J has the minimum distance 17 , it is
selected. The vertex J is removed from set V, and placed in set S.

Graph Algorithms / Dr.A.Sattar /95


Prim’s Algorithm Cont’d-5

Example
Vertex I in V is linked to vertices A and J in S, and has distances 30, 47. Vertex
Vertex D in V is linked to vertices C, J in S, and has distances 45, 57. Vertex E in V is linked
to J in S and has distance 12. Vertex H in S is linked to J in S and distance 39. Vertex K in S is
linked to vertex J in S, and has distance 32. Since E has the minimum distance 12 , it is
selected. The vertex E is removed from set V, and placed in set S.

Graph Algorithms / Dr.A.Sattar /96


Prim’s Algorithm Cont’d-6

Example
Vertex I in V is linked to vertices A and J in S, and has distances 30, 47. Vertex
Vertex D in V is linked to vertices C, J, E in S, and has distances 45, 57,18. H in S is linked
to J in S and has distance 39. Vertex K in S is linked to vertices J and E in S, and has
distances 32, 14. Vertex F in V is linked to E in S and has distance 8. Since F has the
minimum distance 8 , it is selected. The vertex F is removed from set V, and placed in set S.

Graph Algorithms / Dr.A.Sattar /97


Prim’s Algorithm Cont’d-7

Example
Vertex I in V is linked to vertices A and J in S, and has distances 30, 47. Vertex D in V is
linked to vertices C, J, E in S, and has distances 45, 57,18. H in S is linked to J in S and has
distance 39. Vertex K in S is linked to vertices J, E and F in S, and has distances 32, 14, 19 .
Vertex G in V linked to F in S and has distance 50 Since K has the minimum distance 14 , it
is selected. The vertex K is removed from set V, and placed in set S.

Graph Algorithms / Dr.A.Sattar /98


Prim’s Algorithm Cont’d-8

Example
Vertex I in V is linked to vertices A and J in S, and has distances 30, 47. Vertex D in V is
linked to vertices C, J, E in S, and has distances 45, 57,18. H in S is linked to J,K in S and has
distances 39, 29. Vertex G in V linked to F,K in S and has distances 50,55 Since D has the
minimum distance 18 , it is selected. The vertex G is removed from set V, and placed in set S.

Graph Algorithms / Dr.A.Sattar /99


Prim’s Algorithm Cont’d-9

Example
Vertex I in V is linked to vertices A and J in S, and has distances 30, 47. H in S is linked to
J,K in S and has distances 39, 29. Vertex G in V linked to F,K in S and has distances 50,55
Since H has the minimum distance 29 , it is selected. The vertex H is removed from set V,
and placed in set S.

Graph Algorithms / Dr.A.Sattar /100


Prim’s Algorithm Cont’d-10

Example
Vertex I in V is linked to vertices A, J, H in S, and has distances 30, 47, 14. Vertex G in V
linked to F,K,H in S and has distances 50,55,45 Since I has the minimum distance 14 , it is
selected. The vertex I is removed from set V, and placed in set S.

Graph Algorithms / Dr.A.Sattar /101


Prim’s Algorithm Cont’d-11

Example
Vertex G in V linked to F,K,H in S and has distances 50,55,45 Since G has the minimum
distance 45 , it is selected. The vertex G is removed from set V, and placed in set S.
ƒAll the vertices have been processed. The algorithm terminates.
ƒThe total distance of all the selected edges is 182.
ƒ The Minimum Spanning Tree, shown in red, has total length of 182.

Graph Algorithms / Dr.A.Sattar /102


Prim’s Algorithm
Pseudo Code
The MST-PRIM method implements Prim’s algorithm for creating minimum spanning
tree for a graph G. An initial starting vertex s is passed to the method.
MST-PRIM(G, s)
1 T← φ ► T is set of vertices for minimum spanning tree
2 S← φ ► S contains selected vertices
3 S←S U {s} ► starting vertex is added to S
4 V←V-{s} ► Starting vertex is removed fro V
5 while V ≠ φ do
6 d ← ∞ ►d is used to store minimum distance . It is initialized to some very large number
7 for each vertex u∈ S do ►select a vertex u in S
8 for each vertex v ∈ Adj[u] do ►Examine all vertices which are linked to u
9 if d > w(u,v) ► Check if the vertex u has shorter distance
10 then d←w(u,v) ► If yes, replace d with weight w(u,v)
11 w1← v ► store vertex v in w1
12 w2 ← u ►store vertex u in w2
13 V ←V - {w1} ►remove vertex w1 from V
14 S ← S U {w1} ►add vertex w1 to S
15 T ←T U {(w1,w2)} ►Add edge (w1,w2) to T
16 return T

Graph Algorithms / Dr.A.Sattar /103


Shortest Distances

Graph Algorithms / Dr.A.Sattar /104


Single Source Shortest Paths
Algorithm
ƒ The algorithm for single-source shortest paths determines the smallest routes of
all vertices in a graph from a given vertex, called source vertex.

ƒ Figure shows a sample graph and the shortest paths from a vertex A.
Weighted Directed Graph

Shortest paths from vertex A


Source
vertex

Shortest path

ƒThe shortest path algorithm has several applications. It can be used, for example, to find
shortest route between two cities

ƒAn efficient algorithm for single-source shortest path is known as Dijkstra’s algorithm,
named after its inventor.
Graph Algorithms / Dr.A.Sattar /105
Single Source Shortest Paths
Dijkstra’s Algorithm
ƒThe Dijkstra’s algorithm determines shortest paths from a single vertex in a weighted
directed graph. The working of Dijkstra’s algorithm is similar to Prim’s algorithm. It
repeatedly computes the distances between a source vertex and the vertices being explored,
and selects the minimum distance from among the feasible paths. It is categorized as greedy
algorithm.

ƒLet V be the vertex set for a graph G. Let d be an array that stores shortest distances. The
algorithm proceeds as follows:
Step #:1 Initialize elements to some large number

Step # 2: Add source vertex s to an empty set S. Remove s from V.

Step # 3: Repeat Step #:4 through Step #:5 until the set V is empty.

Step # 4: Examine vertices in S which are linked to vertices in V. Choose the vertex u
in V which has the shortest distance from the source. Store this distance in d[u]

Step #5: Remove vertex u from V and add it to S.

Graph Algorithms / Dr.A.Sattar /106


Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /107


Cont’d-1
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /108


Cont’d-2
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /109


Cont’d-3
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /110


Cont’d-4
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /111


Cont’d-5
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /112


Cont’d-6
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /113


Cont’d-7
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /114


Cont’d-8
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /115


Cont’d-9
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /116


Cont’d-10
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /117


Cont’d-11
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /118


Cont’d-12
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /119


Cont’d-13
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /120


Cont’d-14
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /121


Cont’d-15
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /122


Cont’d-16
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /123


Cont’d-17
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /124


Cont’d-18
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /125


Cont’d-19
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /126


Cont’d-20
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /127


Cont’d-21
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /128


Cont’d-22
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /129


Cont’d-23
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /130


Cont’d-24
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /131


Cont’d-25
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /132


Cont’d-26
Dijkstra’s Algorithm
Example

Graph Algorithms / Dr.A.Sattar /133


Dijkstra’s Algorithm
Pseudo Code
The pseudo code for Dijkstra’s algorithm is follows:

DIJKSTRA(G, s)
1 for each vertex v ∈ V
2 do d[v] ← ∞ ► Array d [V\stores minimum distances .
3 S← φ ► S contains selected vertices
4 S←S U {s} ► starting vertex is added to S
5 V←V-{s} ► Starting vertex is removed from V
6 while V ≠ φ do
7 for each vertex u∈ S do ►select a vertex u in S
8 for each vertex v ∈ Adj[u] do ►Examine all vertices which are linked to u
9 if d[u] >d[u]+ w(u,v) ► Check if the d[u] is smaller
10 then d[u]←d[u]+w(u,v) ► If yes, replace d[u] with sum of w(u,v)
11 w← v ► store vertex v in w
12 V ←V - {w} ►remove vertex w from V
13 S ← S U {w} ►add vertex w to S

Visualization

Graph Algorithms / Dr.A.Sattar /134

You might also like