SlideShare a Scribd company logo
Graph ADT BY  Abdul Ghaffar Khan
Contents Basics Representation of of Graph ADT Shortest Path Algorithm Spanning Tree Minimal Spanning Tree Traveling Salesman problem
Basics
Basics: Definition (Directed Graph)   A  directed graph   , or  digraph  , is an ordered pair  with the following properties:  The first component,  , is a finite, non-empty set. The elements of  are called the  vertices  of  G .  The second component,  , is a finite set of ordered pairs of vertices. That is, .  . The elements of  are called the  edges  of  G .  For example, consider the directed graph  comprised of four vertices and six edges :  If pair is the pair is not ordered then it becomes only a Graph but not Directed.
Basics
Basics
Basics
Basics
Basics
Basics
Basics
Basics
Representation: Adjacency Matrix 1 2 3 6 7 5 4
Representation: Adjacency List 1 2 3 4 5 6 7 2 4 3 4 5 7 6 7 4 3 6 6 1 2 3 6 7 5 4
Shortest Path   Unweighted edges Q.Enqueue(v0); while  (!Q.IsEmpty) { V = Q.Dequeue(); for  (Each W adjacent to V)  if (T[W].Dist == Maxint) {   T[W].Dist = T[V].Dist + 1; T[W].Path = V; Q.Enqueue(W);  } } Breadth First Search Algorithm
Shortest Path -  Unweighted edges Queue Enqueue Vo Ignore V4 V4 != MaxInt Ignore V6 V6 != MaxInt Ignore V4 V4 != MaxInt Ignore V7 V7 != MaxInt Ignore V6 V6 != MaxInt Queue is now empty so stop 1 2 3 6 7 5 4 3 6 1 v3 1 1 v3 4 2 v1 2 v1 2 No vertices adjacent to 6 5 v2 3 7 3 v4
Dijkstra’s Algorithm Q.Enqueue(v0); while (!Q.IsEmpty) { do { V = Q.Dequeue(); while (T[V].Known); T[V].Known = true; for (Each W adjacent to V)  if(T[W].Dist > T[V].Dist + C(V,W) { T[W].Dist = T[V].Dist + C(V,W); T[W].Path = V; Q.Enqueue(W);  } } Only accept unknown edges Modify the path if an improvement to dv exists
Dijkstra’s Algorithm cont... 1 2 3 6 7 5 4 4 4 1 1 2 2 2 3 6 5 10 8 PQueue No improvement to v4 so skip No improvement to v1 so skip No improvement to v4 so skip No improvement to v7 so skip Queue is now empty so stop Enqueue Vo 1 0 4 1 1 v1 2 2 2 v1 1 1 3 3 3 v4 7 5 v4 5 6 9 v4 9 1 5 12 v2 12 1 8 v3 6 8 Update dv and pv to reflect improvement 1 6 6 Update dv and pv to reflect improvement v7 6 1 V6 is already known so ignore 1
Definition:
Definition:
Prim’s Algorithm Basic Idea 1. Build a tree starting at Vo=u. 2. Select vertex v which is closest to u and add (u,v) to tree. 3. Find next closes vertex v to tree and add. Repeat 3 Until all v have been consumed.
Prim’s Algorithm Q.Enqueue(V 0 ,V 0 ); Vertices=1 while (Vertices++ < |V|) { do { E = Q.Dequeue(); while (T[v].Known); T[v].Known = true; for (Each w adjacent to v)  if(T[w].Dist > C(v,w) && !T[w].known){ T[w].Dist = C(v,w); T[w].Path = v; Q.Enqueue(v,w);  } } Very Similar to Dijkstra’s Algorithm. dv now only holds the edge weight. PQ of edges c(u,v), where u is in the tree and v is not. Where E = (u,v)
Prim’s Algorithm Cont... C(1,1) PQ 1 2 3 6 7 5 4 4 4 1 2 7 2 3 6 5 10 8 1 1 1 C(1,4) C(1,2) C(1,3) 1 4 1 V1 C(4,1) C(1,2) C(4,3) C(4,2) C(4,6) C(1,3) C(4,7) C(4,5) 2 1 2 V1 C(2,4) C(2,1) C(4,3) C(4,2) C(4,6) C(1,3) C(4,7) C(4,5) C(2,5) 3 1 2 V4 C(2,4) C(3,4) C(2,1) C(4,2) C(4,6) C(1,3) C(4,7) C(4,5) C(2,5) C(3,1) C(3,6) 4 V4 7 1 C(4,6) C(4,5) C(2,5) C(3,1) C(3,6) C(7,5) C(7,4) C(7,6) 6 1 1 V7 C(4,6) C(4,5) C(2,5) C(3,1) C(3,6) C(7,5) C(7,4) C(6,7) C(6,3) C(6,4) 5 1 6 V7
Prim’s Algorithm Cont.. The Algorithm stops when we have accepted |V| vertices. We can read the accepted edges from the table directly.
Traveling Salesman problem The  traveling-salesman problem , which is closely related to the Hamiltonian-cycle problem, a salesman must visit  n  cities. Modeling the problem as a complete graph with  n  vertices, we can say that the salesman wishes to make a  tour , or Hamiltonian cycle, visiting each city exactly once and finishing at the city he starts from. There is an integer cost  c ( i ,  j ) to travel from city  i  to city  j , and the salesman wishes to make the tour whose total cost is minimum, where the total cost is the sum of the individual  costs along the edges of  the tour.
Traveling Salesman problem For example, in  Figure  a minimum-cost tour is  u ,  w ,  v ,  x ,  u , with cost 7.  An instance of the traveling-salesman problem. Shaded edges represent a minimum-cost tour, with cost 7.

More Related Content

PPT
Data structure
PDF
Algorithms of graph
PDF
Bellman ford algorithm
PPTX
Bellmanford . montaser hamza.iraq
PPT
The Floyd–Warshall algorithm
PPT
Bellman Ford's Algorithm
PDF
Skiena algorithm 2007 lecture12 topological sort connectivity
PDF
21 All Pairs Shortest Path
Data structure
Algorithms of graph
Bellman ford algorithm
Bellmanford . montaser hamza.iraq
The Floyd–Warshall algorithm
Bellman Ford's Algorithm
Skiena algorithm 2007 lecture12 topological sort connectivity
21 All Pairs Shortest Path

What's hot (19)

PPTX
Data Algorithms And Analysis
PPTX
Bellman ford algorithm
PPTX
Topological sort
PDF
Topological Sort
PPTX
Bellmanfordwith negative cycle js
PPTX
A presentation on prim's and kruskal's algorithm
PPT
Bellman ford algorithm
PDF
Convex hull
PPTX
Shortest path algorithm
PPTX
convex hull
PPTX
Shortest path problem
PPTX
Quaternion notations
PDF
Convex hulls & Chan's algorithm
DOC
BFS, Breadth first search | Search Traversal Algorithm
PPTX
convex hull
PPT
2.4 mst prim’s algorithm
PPTX
Lecture warshall floyd
PPTX
Trig evaluation of angles
PPT
lecture 21
Data Algorithms And Analysis
Bellman ford algorithm
Topological sort
Topological Sort
Bellmanfordwith negative cycle js
A presentation on prim's and kruskal's algorithm
Bellman ford algorithm
Convex hull
Shortest path algorithm
convex hull
Shortest path problem
Quaternion notations
Convex hulls & Chan's algorithm
BFS, Breadth first search | Search Traversal Algorithm
convex hull
2.4 mst prim’s algorithm
Lecture warshall floyd
Trig evaluation of angles
lecture 21
Ad

Viewers also liked (13)

PDF
Analysis of Algorithms II - PS3
PPT
2.3 shortest path dijkstra’s
PPT
Top-k shortest path
PPT
Unit26 shortest pathalgorithm
PPTX
Multi-core processor and Multi-channel memory architecture
PPT
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
PDF
Intel core i3, i5, i7 , core2 duo and atom processors
PDF
All pairs shortest path algorithm
PPTX
Dijkstra's algorithm
PPT
Intel Core i7 Processors
PPTX
Intel I3,I5,I7 Processor
PPT
Unix command-line tools
PDF
An in-building multi-server cloud system based on shortest Path algorithm dep...
Analysis of Algorithms II - PS3
2.3 shortest path dijkstra’s
Top-k shortest path
Unit26 shortest pathalgorithm
Multi-core processor and Multi-channel memory architecture
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Intel core i3, i5, i7 , core2 duo and atom processors
All pairs shortest path algorithm
Dijkstra's algorithm
Intel Core i7 Processors
Intel I3,I5,I7 Processor
Unix command-line tools
An in-building multi-server cloud system based on shortest Path algorithm dep...
Ad

Similar to Graphs (20)

PPTX
Dijkstra’s Algorithm and Prim’s Algorithm in Graph Theory and Combinatorics
PPT
Directed Acyclic Graph
PPT
bellman-ford Theorem.ppt
PPTX
12_Graph.pptx
PPTX
Introduction to graphs
PPTX
15-bellmanFord.pptx...........................................
PDF
Chap10 slides
PPT
9_graphs2.v4.ppt
PPT
PPT
bellman-ford dynamic algorithm in data structures.ppt
PPTX
Deixtras Algorithm.pptxdjjdjdjdjddddddddddddddd
PDF
JAVA BASED VISUALIZATION AND ANIMATION FOR TEACHING THE DIJKSTRA SHORTEST PAT...
PPT
Prim's Algorithm on minimum spanning tree
PDF
14 chapter9 graph_algorithmstopologicalsort_shortestpath
PDF
P5 - Routing Protocols
PPTX
PPT
Shortest path
PPTX
DIJKSTRA_123.pptx
PDF
Daa chpater14
PPTX
Graph theory
Dijkstra’s Algorithm and Prim’s Algorithm in Graph Theory and Combinatorics
Directed Acyclic Graph
bellman-ford Theorem.ppt
12_Graph.pptx
Introduction to graphs
15-bellmanFord.pptx...........................................
Chap10 slides
9_graphs2.v4.ppt
bellman-ford dynamic algorithm in data structures.ppt
Deixtras Algorithm.pptxdjjdjdjdjddddddddddddddd
JAVA BASED VISUALIZATION AND ANIMATION FOR TEACHING THE DIJKSTRA SHORTEST PAT...
Prim's Algorithm on minimum spanning tree
14 chapter9 graph_algorithmstopologicalsort_shortestpath
P5 - Routing Protocols
Shortest path
DIJKSTRA_123.pptx
Daa chpater14
Graph theory

More from Ghaffar Khan (20)

PPT
World is beautiful ... ...
PPTX
My Presentation On Ajax
PPT
Sorting
PPT
How A Computer Works
PPT
For Loop
PPT
Exponential and Logarthmic funtions
PPT
Exponential and Logarthmic funtions (1)
PPT
Functions
PPT
Quadratic And Polinomial Function
PPT
Quadratic And Polinomial Function
PPT
Exponentioal And Logarthmic Functions
PPT
Internet Protocol
PPT
Introduction to Computer Networks
PPT
Network Layer
PPT
Control Structures
PPT
Input And Output
PPT
Surfaces
PPT
Vector Tools
PPT
Drawing Tools
PPT
Drawing Figures
World is beautiful ... ...
My Presentation On Ajax
Sorting
How A Computer Works
For Loop
Exponential and Logarthmic funtions
Exponential and Logarthmic funtions (1)
Functions
Quadratic And Polinomial Function
Quadratic And Polinomial Function
Exponentioal And Logarthmic Functions
Internet Protocol
Introduction to Computer Networks
Network Layer
Control Structures
Input And Output
Surfaces
Vector Tools
Drawing Tools
Drawing Figures

Recently uploaded (20)

PPTX
Tartificialntelligence_presentation.pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPT
Teaching material agriculture food technology
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Tartificialntelligence_presentation.pptx
cloud_computing_Infrastucture_as_cloud_p
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Teaching material agriculture food technology
Group 1 Presentation -Planning and Decision Making .pptx
A Presentation on Artificial Intelligence
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
OMC Textile Division Presentation 2021.pptx
Heart disease approach using modified random forest and particle swarm optimi...
Unlocking AI with Model Context Protocol (MCP)
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine Learning_overview_presentation.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Assigned Numbers - 2025 - Bluetooth® Document
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Graphs

  • 1. Graph ADT BY Abdul Ghaffar Khan
  • 2. Contents Basics Representation of of Graph ADT Shortest Path Algorithm Spanning Tree Minimal Spanning Tree Traveling Salesman problem
  • 4. Basics: Definition (Directed Graph)   A directed graph   , or digraph  , is an ordered pair with the following properties: The first component, , is a finite, non-empty set. The elements of are called the vertices of G . The second component, , is a finite set of ordered pairs of vertices. That is, . . The elements of are called the edges of G . For example, consider the directed graph comprised of four vertices and six edges : If pair is the pair is not ordered then it becomes only a Graph but not Directed.
  • 14. Representation: Adjacency List 1 2 3 4 5 6 7 2 4 3 4 5 7 6 7 4 3 6 6 1 2 3 6 7 5 4
  • 15. Shortest Path Unweighted edges Q.Enqueue(v0); while (!Q.IsEmpty) { V = Q.Dequeue(); for (Each W adjacent to V) if (T[W].Dist == Maxint) { T[W].Dist = T[V].Dist + 1; T[W].Path = V; Q.Enqueue(W); } } Breadth First Search Algorithm
  • 16. Shortest Path - Unweighted edges Queue Enqueue Vo Ignore V4 V4 != MaxInt Ignore V6 V6 != MaxInt Ignore V4 V4 != MaxInt Ignore V7 V7 != MaxInt Ignore V6 V6 != MaxInt Queue is now empty so stop 1 2 3 6 7 5 4 3 6 1 v3 1 1 v3 4 2 v1 2 v1 2 No vertices adjacent to 6 5 v2 3 7 3 v4
  • 17. Dijkstra’s Algorithm Q.Enqueue(v0); while (!Q.IsEmpty) { do { V = Q.Dequeue(); while (T[V].Known); T[V].Known = true; for (Each W adjacent to V) if(T[W].Dist > T[V].Dist + C(V,W) { T[W].Dist = T[V].Dist + C(V,W); T[W].Path = V; Q.Enqueue(W); } } Only accept unknown edges Modify the path if an improvement to dv exists
  • 18. Dijkstra’s Algorithm cont... 1 2 3 6 7 5 4 4 4 1 1 2 2 2 3 6 5 10 8 PQueue No improvement to v4 so skip No improvement to v1 so skip No improvement to v4 so skip No improvement to v7 so skip Queue is now empty so stop Enqueue Vo 1 0 4 1 1 v1 2 2 2 v1 1 1 3 3 3 v4 7 5 v4 5 6 9 v4 9 1 5 12 v2 12 1 8 v3 6 8 Update dv and pv to reflect improvement 1 6 6 Update dv and pv to reflect improvement v7 6 1 V6 is already known so ignore 1
  • 21. Prim’s Algorithm Basic Idea 1. Build a tree starting at Vo=u. 2. Select vertex v which is closest to u and add (u,v) to tree. 3. Find next closes vertex v to tree and add. Repeat 3 Until all v have been consumed.
  • 22. Prim’s Algorithm Q.Enqueue(V 0 ,V 0 ); Vertices=1 while (Vertices++ < |V|) { do { E = Q.Dequeue(); while (T[v].Known); T[v].Known = true; for (Each w adjacent to v) if(T[w].Dist > C(v,w) && !T[w].known){ T[w].Dist = C(v,w); T[w].Path = v; Q.Enqueue(v,w); } } Very Similar to Dijkstra’s Algorithm. dv now only holds the edge weight. PQ of edges c(u,v), where u is in the tree and v is not. Where E = (u,v)
  • 23. Prim’s Algorithm Cont... C(1,1) PQ 1 2 3 6 7 5 4 4 4 1 2 7 2 3 6 5 10 8 1 1 1 C(1,4) C(1,2) C(1,3) 1 4 1 V1 C(4,1) C(1,2) C(4,3) C(4,2) C(4,6) C(1,3) C(4,7) C(4,5) 2 1 2 V1 C(2,4) C(2,1) C(4,3) C(4,2) C(4,6) C(1,3) C(4,7) C(4,5) C(2,5) 3 1 2 V4 C(2,4) C(3,4) C(2,1) C(4,2) C(4,6) C(1,3) C(4,7) C(4,5) C(2,5) C(3,1) C(3,6) 4 V4 7 1 C(4,6) C(4,5) C(2,5) C(3,1) C(3,6) C(7,5) C(7,4) C(7,6) 6 1 1 V7 C(4,6) C(4,5) C(2,5) C(3,1) C(3,6) C(7,5) C(7,4) C(6,7) C(6,3) C(6,4) 5 1 6 V7
  • 24. Prim’s Algorithm Cont.. The Algorithm stops when we have accepted |V| vertices. We can read the accepted edges from the table directly.
  • 25. Traveling Salesman problem The traveling-salesman problem , which is closely related to the Hamiltonian-cycle problem, a salesman must visit n cities. Modeling the problem as a complete graph with n vertices, we can say that the salesman wishes to make a tour , or Hamiltonian cycle, visiting each city exactly once and finishing at the city he starts from. There is an integer cost c ( i , j ) to travel from city i to city j , and the salesman wishes to make the tour whose total cost is minimum, where the total cost is the sum of the individual costs along the edges of the tour.
  • 26. Traveling Salesman problem For example, in Figure a minimum-cost tour is u , w , v , x , u , with cost 7. An instance of the traveling-salesman problem. Shaded edges represent a minimum-cost tour, with cost 7.