0% found this document useful (0 votes)
10 views25 pages

Ds 15warshall

Uploaded by

Aryan Gour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

Ds 15warshall

Uploaded by

Aryan Gour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

 Introduction

 Warshall's algorithm
 Shortest path algorithm Floyd Warshall
Algorithm (modified warshall algorithm)
What is a Graph?
 A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
 An edge e = (u,v) is a pair of vertices
 Example:

a b V= {a,b,c,d,e}

E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
c (d,e)}

d e
Graphs - Introduction
Graph is basically a collection of vertices (also called
nodes) and edges that connect these vertices.

For example: Family trees in which the member nodes


have an edge from parent to each of their children.

Graphs are widely used to model any situation where


entities or things are related to each other in pairs.
Definition
A graph G is defined as an ordered set
(V, E), where V(G) represents the set
of vertices and E(G) represents the
edges that connect these vertices.

Figure shows a graph with V(G) = {A,


B, C, D and E} and E(G) = {(A, B), (B,
C),(A, D), (B, D), (D, E), (C, E)}.

There are five vertices or nodes and


six edges in the graph.
Representation of Graphs
The common ways of storing graphs in the computer’s
memory are:

Sequential representation :by using an adjacency


matrix.

Linked representation: by using an adjacency list


that stores the neighbors of a node using a linked list.
Adjacency Matrix Representation
An adjacency matrix is used to represent which nodes are
adjacent to one another.

By definition, two nodes are said to be adjacent if there is


an edge connecting them.

In an adjacency matrix, the rows and columns are labelled


by graph vertices.

Since an adjacency matrix contains only 0s and 1s, it is


called a bit matrix or a Boolean matrix. The entries in
the matrix depend on the ordering of the nodes in G.
Adjacency List Representation
It consists of a list of all nodes in G. Furthermore, every
node is in turn linked to its own list that contains the
names of all other nodes that are adjacent to it.
Node List: Node, Next and Adjacent
Edge List: Destination and Link
The key advantages of using an adjacency list are:
It is easy to follow and clearly shows the adjacent nodes
of a particular node.
It is often used for storing graphs that have a small-to-
moderate number of edges.
Adding new nodes in G is easy and straightforward
when G is represented using an adjacency list.
Adding new nodes in an adjacency matrix is a difficult
task, as the size of the matrix needs to be changed
and existing nodes may have to be reordered.
Complete graph: no of edges needed: n(n-1)/2
Where n is no. of vertices
Array representation complexity: O(V^2)
so better to represent Complete graph
LL representation complexity: O(V+E)
- so better to represent sparse graph

Sparse graph :Sparse graph is a graph in which the


number of edges is close to the minimal number of
edges. Sparse graph can be a disconnected graph
Graph Terminology
Directed Graph
1.Adjacenet nodes or neighbors
2.Degree(Indegree and Outdegree)
3.Reachable node
4.Connected Graph(path exist)
5.Strongly connected Graph(if edge x to y then there
should exist from y to x also)
6.Source (The vertex whose indegree=0)
7.Sink(The vertex whose outdegree=0)
Adjacency Matrix and Path Matrix
Y X

Z W

0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 1
1 0 1 1 1 0 1 2 1 0 2 2 2 0 2 3
A1 = A2 = A3 = A4 =
1 0 0 1 0 0 1 1 1 0 1 1 1 0 1 2
0 0 1 0 1 0 0 1 0 0 1 1 1 0 1 1

Number of Paths between Y to W of length 3 are 2


(Y -> Z -> X -> W AND Y->W->Z->W)
B 4 = A 1 + A 2 + A 3 + A4
• entry of matrix B4 gives the number of paths of length 4 or less from node vi to vj

• entry of matrix Br gives the number of paths of length r or less from node vi to vj
Warshall's algorithm
1. Repeat for I, J = 1, 2,…, M: [Initializes P.]
If A[I, J] = 0, then: Set P[I, J] := 0;
Else Set P[I, J] := 1.
[End of loop.]
2. Repeat Steps 3 and 4 for K = 1, 2,…, M: [Updates P.]
3. Repeat Step 4 for I = 1, 2,…, M:
4. Repeat J= 1, 2,…, M:
Set P[I, J] := P[I, J] OR (P[I, K] AND P[K, J] ).
[End of loop.]
[End of Step 3 loop.]
[End of step 2 loop.]
5. Exit
Example:
Floyd Warshall Algorithm
1. Repeat for I, J = 1, 2,…, M: [Initializes P.]
If A[I, J] = 0, then: Set P[I, J] := ∞;
Else Set P[I, J] := W(I, J).
[End of loop.]
2. Repeat Steps 3 and 4 for K = 1, 2,…, M: [Updates P.]
3. Repeat Step 4 for I = 1, 2,…, M:
4. Repeat J= 1, 2,…, M:
Set P[I, J] := MIN(P[I, J], (P[I, K]+P[K, J])).
[End of loop.]
[End of Step 3 loop.]
[End of step 2 loop.]
5. Exit
Example:
k You
Th an

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy