# GRAPHS Prof. Muhammad Saeed Analysis of Algorithms Analysis Of Algorithms1.

## Presentation on theme: "GRAPHS Prof. Muhammad Saeed Analysis of Algorithms Analysis Of Algorithms1."— Presentation transcript:

GRAPHS Prof. Muhammad Saeed Analysis of Algorithms Analysis Of Algorithms1

2

3

4

5

Topological Sort Analysis Of Algorithms6

Indegree Before Dequeue Vertex 1234567 V1V1 0 V2V2 1 V3V3 2 V4V4 3 V5V5 1 V6V6 3 V7V7 2 enqueueV1V1 dequeueV1V1 Topological Sort Step by Step Step 1 Analysis Of Algorithms7

Indegree Before Dequeue Vertex 1234567 V1V1 0 V2V2 1 V3V3 2 V4V4 3 V5V5 1 V6V6 3 V7V7 2 enqueueV1V1 dequeueV1V1 Topological Sort 0 0 1 2 1 3 2 V2V2 V2V2 Step by Step Step 2 Analysis Of Algorithms8

Indegree Before Dequeue Vertex 1234567 V1V1 0 V2V2 1 V3V3 2 V4V4 3 V5V5 1 V6V6 3 V7V7 2 enqueueV1V1 dequeueV1V1 Topological Sort 0 0 1 2 1 3 2 V2V2 V2V2 0 0 1 1 0 3 2 V5V5 V5V5 Step by Step Step 3 Analysis Of Algorithms9

Indegree Before Dequeue Vertex 1234567 V1V1 0 V2V2 1 V3V3 2 V4V4 3 V5V5 1 V6V6 3 V7V7 2 enqueueV1V1 dequeueV1V1 Topological Sort 0 0 1 2 1 3 2 V2V2 V2V2 0 0 1 1 0 3 2 V5V5 V5V5 0 0 1 0 0 3 1 V4V4 V4V4 Step by Step Step 4 Analysis Of Algorithms10

Indegree Before Dequeue Vertex 1234567 V1V1 0 V2V2 1 V3V3 2 V4V4 3 V5V5 1 V6V6 3 V7V7 2 enqueueV1V1 dequeueV1V1 Topological Sort 0 0 1 2 1 3 2 V2V2 V2V2 0 0 1 1 0 3 2 V5V5 V5V5 0 0 1 0 0 3 1 V4V4 V4V4 0 0 0 0 0 2 0 V 3, V 7 V3V3 Step by Step Step 5 Analysis Of Algorithms11

Indegree Before Dequeue Vertex 1234567 V1V1 0 V2V2 1 V3V3 2 V4V4 3 V5V5 1 V6V6 3 V7V7 2 enqueueV1V1 dequeueV1V1 Topological Sort 0 0 1 2 1 3 2 V2V2 V2V2 0 0 1 1 0 3 2 V5V5 V5V5 0 0 1 0 0 3 1 V4V4 V4V4 0 0 0 0 0 2 0 V 3, V 7 V3V3 0 0 0 0 0 1 0 V7V7 Step by Step Step 6 Analysis Of Algorithms12

Indegree Before Dequeue Vertex 1234567 V1V1 0 V2V2 1 V3V3 2 V4V4 3 V5V5 1 V6V6 3 V7V7 2 enqueueV1V1 dequeueV1V1 Topological Sort 0 0 1 2 1 3 2 V2V2 V2V2 0 0 1 1 0 3 2 V5V5 V5V5 0 0 1 0 0 3 1 V4V4 V4V4 0 0 0 0 0 2 0 V 3, V 7 V3V3 0 0 0 0 0 1 0 V7V7 0 0 0 0 0 0 0 V6V6 V6V6 Step by Step Step 7 Analysis Of Algorithms13

Indegree Before Dequeue Vertex 1234567 V1V1 0 V2V2 1 V3V3 2 V4V4 3 V5V5 1 V6V6 3 V7V7 2 enqueueV1V1 dequeueV1V1 Topological Sort 0 0 1 2 1 3 2 V2V2 V2V2 0 0 1 1 0 3 2 V5V5 V5V5 0 0 1 0 0 3 1 V4V4 V4V4 0 0 0 0 0 2 0 V 3, V 7 V3V3 0 0 0 0 0 1 0 V7V7 0 0 0 0 0 0 0 V6V6 V6V6 Analysis Of Algorithms14

Analysis Of Algorithms15 Complexity Analysis Topological Sort

Shortest Path Algorithm Unweighted Graphs Analysis Of Algorithms16

Shortest Path Algorithm Unweighted Graphs Initial State v Knowndvdv pvpv v1v1 0 0 v2v2 0 0 v3v3 000 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 Q v3v3 Step by Step Step 1 Analysis Of Algorithms17

Shortest Path Algorithm Unweighted Graphs Initial State v Knowndvdv pvpv v1v1 0 0 v2v2 0 0 v3v3 000 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 Q v3v3 v 3 dequeued Knowndvdv pvpv 01v3v3 0 0 100 0 0 0 0 01v3v3 0 0 v 1, v 6 Step by Step Step 2 Analysis Of Algorithms18

Shortest Path Algorithm Unweighted Graphs Initial State v Knowndvdv pvpv v1v1 0 0 v2v2 0 0 v3v3 000 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 Q v3v3 v 3 dequeued Knowndvdv pvpv 01v3v3 0 0 100 0 0 0 0 01v3v3 0 0 v 1, v 6 v 1 dequeued Knowndvdv pvpv 11v3v3 02v1v1 100 02v1v1 0 0 01v3v3 0 0 v 6, v 2, v 4 Step by Step Step 3 Analysis Of Algorithms19

Shortest Path Algorithm Unweighted Graphs Initial State v Knowndvdv pvpv v1v1 0 0 v2v2 0 0 v3v3 000 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 Q v3v3 v 3 dequeued Knowndvdv pvpv 01v3v3 0 0 100 0 0 0 0 01v3v3 0 0 v 1, v 6 v 1 dequeued Knowndvdv pvpv 11v3v3 02v1v1 100 02v1v1 0 0 01v3v3 0 0 v 6, v 2, v 4 v 6 dequeued Knowndvdv pvpv 11v3v3 02v1v1 100 02v1v1 0 0 11v3v3 0 0 v 2, v 4 Step by Step Step 4 Analysis Of Algorithms20

Shortest Path Algorithm Unweighted Graphs v 2 dequeued v Knowndvdv pvpv v1v1 11v3v3 v2v2 12v1v1 v3v3 100 v4v4 02v1v1 v5v5 03v2v2 v6v6 11v3v3 v7v7 0 0 Q v 4, v 5 v 6 dequeued Knowndvdv pvpv 11v3v3 02v1v1 100 02v1v1 0 0 11v3v3 0 0 v 2, v 4 Step by Step Step 5 Analysis Of Algorithms21

Shortest Path Algorithm Unweighted Graphs v 2 dequeued v Knowndvdv pvpv v1v1 11v3v3 v2v2 12v1v1 v3v3 100 v4v4 02v1v1 v5v5 03v2v2 v6v6 11v3v3 v7v7 0 0 Q v 4, v 5 v 4 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 03v2v2 11v3v3 03v4v4 v 5, v 7 v 6 dequeued Knowndvdv pvpv 11v3v3 02v1v1 100 02v1v1 0 0 11v3v3 0 0 v 2, v 4 Step by Step Step 6 Analysis Of Algorithms22

Shortest Path Algorithm Unweighted Graphs v 2 dequeued v Knowndvdv pvpv v1v1 11v3v3 v2v2 12v1v1 v3v3 100 v4v4 02v1v1 v5v5 03v2v2 v6v6 11v3v3 v7v7 0 0 Q v 4, v 5 v 4 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 03v2v2 11v3v3 03v4v4 v 5, v 7 v 5 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 13v2v2 11v3v3 03v4v4 v7v7 v 6 dequeued Knowndvdv pvpv 11v3v3 02v1v1 100 02v1v1 0 0 11v3v3 0 0 v 2, v 4 Step by Step Step 7 Analysis Of Algorithms23

Shortest Path Algorithm Unweighted Graphs v 2 dequeued v Knowndvdv pvpv v1v1 11v3v3 v2v2 12v1v1 v3v3 100 v4v4 02v1v1 v5v5 03v2v2 v6v6 11v3v3 v7v7 0 0 Q v 4, v 5 v 4 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 03v2v2 11v3v3 03v4v4 v 5, v 7 v 5 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 13v2v2 11v3v3 03v4v4 v7v7 v 7 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 13v2v2 11v3v3 13v4v4 empty v 6 dequeued Knowndvdv pvpv 11v3v3 02v1v1 100 02v1v1 0 0 11v3v3 0 0 v 2, v 4 Step by Step Step 8 Analysis Of Algorithms24

Shortest Path Algorithm Unweighted Graphs v 2 dequeued v Knowndvdv pvpv v1v1 11v3v3 v2v2 12v1v1 v3v3 100 v4v4 02v1v1 v5v5 03v2v2 v6v6 11v3v3 v7v7 0 0 Q v 4, v 5 v 4 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 03v2v2 11v3v3 03v4v4 v 5, v 7 v 5 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 13v2v2 11v3v3 03v4v4 v7v7 v 7 dequeued Knowndvdv pvpv 11v3v3 12v1v1 100 12v1v1 13v2v2 11v3v3 13v4v4 empty v 6 dequeued Knowndvdv pvpv 11v3v3 02v1v1 100 02v1v1 0 0 11v3v3 0 0 v 2, v 4 n = |V| T(n) = O(n 2 ) for arrays T(n) = O(|V| + |E|) for adjacency List Analysis Of Algorithms25

Shortest Path Algorithm Unweighted Graphs Analysis Of Algorithms26 Complexity Analysis T(n) = O(n 2 ) for arrays T(n) = O(|V| + |E|) for adjacency List

Analysis Of Algorithms27 END OF ( Shortest Path Unweighted Graphs ) Algorithm

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm Analysis Of Algorithms28

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm Initial State v Knowndvdv pvpv v1v1 000 v2v2 0 0 v3v3 0 0 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 Q v1v1 Step by Step Step 1 Analysis Of Algorithms29

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm Initial State v Knowndvdv pvpv v1v1 000 v2v2 0 0 v3v3 0 0 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 Q v1v1 v 1 dequeued Knowndvdv pvpv 100 02v1v1 000 01v1v1 0 0 0 0 0 0 v 2, v 4 Step by Step Step 2 Analysis Of Algorithms30

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm v 2 dequeued Knowndvdv pvpv 100 12v1v1 03v4v4 01v1v1 03v4v4 09v4v4 05v4v4 v 2, v 5, v 3, v 6, v 7, Step by Step Step 3 v 1 dequeued Knowndvdv pvpv 100 02v1v1 000 01v1v1 0 0 0 0 0 0 v 4, v 2 Initial State v Knowndvdv pvpv v1v1 000 v2v2 0 0 v3v3 0 0 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 Q v1v1 Analysis Of Algorithms31

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm v 2 dequeued Knowndvdv pvpv 100 12v1v1 000 01v1v1 012v2v2 0 0 0 0 v 4, v 5 Step by Step Step 4 v 1 dequeued Knowndvdv pvpv 100 02v1v1 000 01v1v1 0 0 0 0 0 0 v 2, v 4 Initial State v Knowndvdv pvpv v1v1 000 v2v2 0 0 v3v3 0 0 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 Q v1v1 v 4 dequeued Knowndvdv pvpv 100 12v1v1 03v4v4 11v1v1 03v4v4 09v4v4 05v4v4 v 5, v 3, v 6,v 7 Analysis Of Algorithms32

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm Step by Step Step 5 v 5 dequeued v Knowndvdv pvpv v1v1 100 v2v2 12v1v1 v3v3 03v4v4 v4v4 11v1v1 v5v5 13v4v4 v6v6 09v4v4 v7v7 05v4v4 Q v 3, v 6,v 7 v 4 dequeued Knowndvdv pvpv 100 12v1v1 030 11v1v1 03v4v4 09v4v4 05v4v4 v 5, v 3, v 6,v 7 Analysis Of Algorithms33

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm Step by Step Step 6 v 3 dequeued Knowndvdv pvpv 100 12v1v1 13v4v4 11v1v1 13v4v4 08v3v3 05v4v4 v6,v7v6,v7 v 5 dequeued v Knowndvdv pvpv v1v1 100 v2v2 12v1v1 v3v3 030 v4v4 11v1v1 v5v5 13v4v4 v6v6 09v4v4 v7v7 05v4v4 Q v 3, v 6,v 7 v 4 dequeued Knowndvdv pvpv 100 12v1v1 030 11v1v1 03v4v4 09v4v4 05v4v4 v 5, v 3, v 6,v 7 Analysis Of Algorithms34

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm v 6 dequeued Knowndvdv pvpv 100 12v1v1 13v4v4 11v1v1 13v4v4 18v3v3 05v4v4 v7v7 Step by Step Step 7 v 3 dequeued Knowndvdv pvpv 100 12v1v1 130 11v1v1 13v4v4 08v3v3 05v4v4 v6,v7v6,v7 v 5 dequeued v Knowndvdv pvpv v1v1 100 v2v2 12v1v1 v3v3 030 v4v4 11v1v1 v5v5 13v4v4 v6v6 09v4v4 v7v7 05v4v4 Q v 3, v 6,v 7 v 4 dequeued Knowndvdv pvpv 100 12v1v1 030 11v1v1 03v4v4 09v4v4 05v4v4 v 5, v 3, v 6,v 7 Analysis Of Algorithms35

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm v 6 dequeued Knowndvdv pvpv 100 12v1v1 130 11v1v1 13v4v4 18v3v3 05v4v4 v7v7 Step by Step Step 8 v 3 dequeued Knowndvdv pvpv 100 12v1v1 130 11v1v1 13v4v4 08v3v3 05v4v4 v6,v7v6,v7 v 5 dequeued v Knowndvdv pvpv v1v1 100 v2v2 12v1v1 v3v3 030 v4v4 11v1v1 v5v5 13v4v4 v6v6 09v4v4 v7v7 05v4v4 Q v 3, v 6,v 7 v 4 dequeued Knowndvdv pvpv 100 12v1v1 030 11v1v1 03v4v4 09v4v4 05v4v4 v 5, v 3, v 6,v 7 v 7 dequeued Knowndvdv pvpv 100 12v1v1 13v4v4 11v1v1 13v4v4 16v7v7 15v4v4 Empty Analysis Of Algorithms36

Shortest Path Algorithm Weighted Graphs Dijkstras Algorithm Analysis Of Algorithms37 Complexity Analysis T(n) = O(V 2 ) for Arrays T(n) = O(|E| log(|V|) for Binary Minimum Heap T(n) = O(|E| +|V|log(|V|) ) for Fibonacci Heap

Analysis Of Algorithms38 END OF Dijkstras Algorithm

Analysis Of Algorithms39 Shortest Path Algorithm Negative Weighted Graphs Bellman-Fords Algorithm

Analysis Of Algorithms40 BELLMAN-FORD (G, w, s) INITIALIZE-SINGLE-SOURCE (G, s) for each vertex i = 1 to V[G] - 1 do for each edge (u, v) in E[G] do RELAX (u, v, w) For each edge (u, v) in E[G] do if d[u] + w(u, v) < d[v] then return FALSE return TRUE

Analysis Of Algorithms41 s zy 6 7 8 -3 7 2 9 -2 x t -4 5 ………….Bellman-Fords Algorithm

Analysis Of Algorithms42 s zy 6 7 8 -3 7 2 9 -2 x t -4 5 s zy 6 7 8 -3 7 2 9 -2 x t -4 5 ………….Bellman-Fords Algorithm

Analysis Of Algorithms43 s zy 6 7 8 -3 7 2 9 -2 x t -4 5 s zy 6 7 8 -3 7 2 9 -2 x t -4 5 ………….Bellman-Fords Algorithm

Analysis Of Algorithms44 s zy 6 7 8 -3 7 2 9 -2 x t -4 s zy 6 7 8 -3 7 2 9 -2 x t -4 5 ………….Bellman-Fords Algorithm

Analysis Of Algorithms45 Complexity Analysis T(n) = O(VE) Bellman-Ford Algorithm

Analysis Of Algorithms46 END OF Bellman-Ford Algorithm A C B ED 2 4 -3 23 53 4 π: nil d: 0 π: nil A d: -1 π: nil C d: 2 π: nil A d: 4 π: nil d:

Minimum Spanning Tree Weighted and Undirected Graphs Prims Algorithm Analysis Of Algorithms47

Prims Algorithm Initial State v Knowndvdv pvpv v1v1 000 v2v2 0 0 v3v3 0 0 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 v1v1 Step by Step Step 1 Minimum Spanning Tree Weighted and Undirected Graphs Analysis Of Algorithms48

Initial State v Knowndvdv pvpv v1v1 000 v2v2 0 0 v3v3 0 0 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 v1v1 Minimum Spanning Tree Weighted and Undirected Graphs v 1 known Knowndvdv pvpv 100 02v1v1 04v1v1 01v1v1 0 0 0 0 0 0 v 2, v 3,v 4 Prims Algorithm Step by Step Step 2 Analysis Of Algorithms49

Initial State v Knowndvdv pvpv v1v1 000 v2v2 0 0 v3v3 0 0 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 v1v1 Minimum Spanning Tree Weighted and Undirected Graphs v 1 known Knowndvdv pvpv 100 02v1v1 04v1v1 01v1v1 0 0 0 0 0 0 v 2, v 3,v 4 Prims Algorithm Step by Step Step 3 v 4 known Knowndvdv pvpv 100 02v1v1 02v4v4 11v1v1 07v4v4 08v4v4 04v4v4 v 2,v 3, v 5, v 6, v 7 Analysis Of Algorithms50

Initial State v Knowndvdv pvpv v1v1 000 v2v2 0 0 v3v3 0 0 v4v4 0 0 v5v5 0 0 v6v6 0 0 v7v7 0 0 v1v1 Minimum Spanning Tree Weighted and Undirected Graphs v 1 known Knowndvdv pvpv 100 02v1v1 04v1v1 01v1v1 0 0 0 0 0 0 v 2, v 3,v 4 Prims Algorithm Step by Step Step 4 v 4 known Knowndvdv pvpv 100 02v1v1 02v4v4 11v1v1 07v4v4 08v4v4 04v4v4 v 2,v 3, v 5, v 6, v 7 v 2 known Knowndvdv pvpv 100 12v1v1 02v4v4 11v1v1 07v4v4 08v4v4 04v4v4 v 3, v 5, v 6, v 7 Analysis Of Algorithms51

v 3 known v Knowndvdv pvpv v1v1 100 v2v2 12v1v1 v3v3 12v4v4 v4v4 11v1v1 v5v5 07v4v4 v6v6 05v3v3 v7v7 04v4v4 v 5, v 6, v 7 Minimum Spanning Tree Weighted and Undirected Graphs Prims Algorithm Step by Step Step 5 v 2 known Knowndvdv pvpv 100 12v1v1 02v4v4 11v1v1 07v4v4 08v4v4 04v4v4 v 3, v 5, v 6, v 7 Analysis Of Algorithms52

v 3 known v Knowndvdv pvpv v1v1 100 v2v2 12v1v1 v3v3 12v4v4 v4v4 11v1v1 v5v5 07v4v4 v6v6 05v3v3 v7v7 04v4v4 v 5, v 6, v 7 Minimum Spanning Tree Weighted and Undirected Graphs v 7 known Knowndvdv pvpv 100 12v1v1 12v4v4 11v1v1 06v7v7 01v7v7 14v4v4 v 5, v 6 Prims Algorithm Step by Step Step 6 v 2 known Knowndvdv pvpv 100 12v1v1 02v4v4 11v1v1 07v4v4 08v4v4 04v4v4 v 3, v 5, v 6, v 7 Analysis Of Algorithms53

v 3 known v Knowndvdv pvpv v1v1 100 v2v2 12v1v1 v3v3 12v4v4 v4v4 11v1v1 v5v5 07v4v4 v6v6 05v3v3 v7v7 04v4v4 v 5, v 6, v 7 Minimum Spanning Tree Weighted and Undirected Graphs v 7 known Knowndvdv pvpv 100 12v1v1 12v4v4 11v1v1 06v7v7 01v7v7 14v4v4 v 5, v 6 Prims Algorithm Step by Step Step 7 v 6 known Knowndvdv pvpv 100 12v1v1 12v4v4 11v1v1 06v7v7 11v7v7 14v4v4 v5v5 v 2 known Knowndvdv pvpv 100 12v1v1 02v4v4 11v1v1 07v4v4 08v4v4 04v4v4 v 3, v 5, v 6, v 7 Analysis Of Algorithms54

Minimum Spanning Tree Weighted and Undirected Graphs Prims Algorithm Step by Step Step 8 v 5 known Knowndvdv pvpv 100 12v1v1 12v4v4 11v1v1 16v7v7 11v7v7 14v4v4 v 2 known Knowndvdv pvpv 100 12v1v1 02v4v4 11v1v1 07v4v4 08v4v4 04v4v4 v 3, v 5, v 6, v 7 v 3 known v Knowndvdv pvpv v1v1 100 v2v2 12v1v1 v3v3 12v4v4 v4v4 11v1v1 v5v5 07v4v4 v6v6 05v3v3 v7v7 04v4v4 v 5, v 6, v 7 v 7 known Knowndvdv pvpv 100 12v1v1 12v4v4 11v1v1 06v7v7 01v7v7 14v4v4 v 5, v 6 v 6 known Knowndvdv pvpv 100 12v1v1 12v4v4 11v1v1 06v7v7 11v7v7 14v4v4 v5v5 Total Cost = 16 Analysis Of Algorithms55

Prims Algorithm Analysis Of Algorithms56 Complexity Analysis

Analysis Of Algorithms57 END OF Prims Algorithm

Minimum Spanning Tree Weighted and Undirected Graphs Kruskals Algorithm Analysis Of Algorithms58

Kruskals Algorithm Step by Step Step 1 Minimum Spanning Tree Weighted Undirected Graphs Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Analysis Of Algorithms59

Minimum Spanning Tree Weighted and Undirected Graphs Kruskals Algorithm Analysis Of Algorithms60 Complexity Analysis T(n) = O(|E|log(|E|) ) T(n) = O(|E|log(|V|) )

Analysis Of Algorithms61 End of Kruskals Algorithm

End End GRAPHS Analysis Of Algorithms62

Similar presentations