Your SlideShare is downloading. ×
0
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Graphs
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Graphs

825

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
825
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Graph ADT BY Abdul Ghaffar Khan
  • 2. Contents <ul><li>Basics </li></ul><ul><li>Representation of of Graph ADT </li></ul><ul><li>Shortest Path Algorithm </li></ul><ul><li>Spanning Tree </li></ul><ul><li>Minimal Spanning Tree </li></ul><ul><li>Traveling Salesman problem </li></ul>
  • 3. Basics
  • 4. Basics: <ul><li>Definition (Directed Graph)   A directed graph   , or digraph  , is an ordered pair with the following properties: </li></ul><ul><ul><li>The first component, , is a finite, non-empty set. The elements of are called the vertices of G . </li></ul></ul><ul><ul><li>The second component, , is a finite set of ordered pairs of vertices. That is, . . The elements of are called the edges of G . </li></ul></ul><ul><li>For example, consider the directed graph comprised of four vertices and six edges : </li></ul>If pair is the pair is not ordered then it becomes only a Graph but not Directed.
  • 5. Basics
  • 6. Basics
  • 7. Basics
  • 8. Basics
  • 9. Basics
  • 10. Basics
  • 11. Basics
  • 12. Basics
  • 13. Representation: Adjacency Matrix 1 2 3 6 7 5 4
  • 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 <ul><li>Q.Enqueue(v0); </li></ul><ul><li>while (!Q.IsEmpty) { </li></ul><ul><li>V = Q.Dequeue(); </li></ul><ul><li>for (Each W adjacent to V) </li></ul><ul><li>if (T[W].Dist == Maxint) { </li></ul><ul><li> T[W].Dist = T[V].Dist + 1; </li></ul><ul><li>T[W].Path = V; </li></ul><ul><li>Q.Enqueue(W); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>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 <ul><li>Q.Enqueue(v0); </li></ul><ul><li>while (!Q.IsEmpty) { </li></ul><ul><li>do { </li></ul><ul><li>V = Q.Dequeue(); </li></ul><ul><li>while (T[V].Known); </li></ul><ul><li>T[V].Known = true; </li></ul><ul><li>for (Each W adjacent to V) </li></ul><ul><li>if(T[W].Dist > T[V].Dist + C(V,W) { </li></ul><ul><li>T[W].Dist = T[V].Dist + C(V,W); </li></ul><ul><li>T[W].Path = V; </li></ul><ul><li>Q.Enqueue(W); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>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
  • 19. Definition:
  • 20. Definition:
  • 21. Prim’s Algorithm <ul><li>Basic Idea </li></ul><ul><ul><li>1. Build a tree starting at Vo=u. </li></ul></ul><ul><ul><li>2. Select vertex v which is closest to u and add (u,v) to tree. </li></ul></ul><ul><ul><li>3. Find next closes vertex v to tree and add. </li></ul></ul><ul><ul><li>Repeat 3 Until all v have been consumed. </li></ul></ul>
  • 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.. <ul><li>The Algorithm stops when we have accepted |V| vertices. </li></ul><ul><li>We can read the accepted edges from the table directly. </li></ul>
  • 25. Traveling Salesman problem <ul><li>The traveling-salesman problem , which is closely related to the Hamiltonian-cycle problem, </li></ul><ul><li>a salesman must visit n cities. Modeling the problem as a complete graph with n vertices, we </li></ul><ul><li>can say that the salesman wishes to make a tour , or Hamiltonian cycle, visiting each city </li></ul><ul><li>exactly once and finishing at the city he starts from. There is an integer cost c ( i , j ) to travel </li></ul><ul><li>from city i to city j , and the salesman wishes to make the tour whose total cost is minimum, </li></ul><ul><li>where the total cost is the sum of the </li></ul><ul><li>individual costs along the edges of </li></ul><ul><li>the tour. </li></ul>
  • 26. Traveling Salesman problem <ul><li>For example, in Figure a minimum-cost tour is u , w , v , x , u , with cost 7. </li></ul><ul><li>An instance of the traveling-salesman problem. Shaded edges represent a </li></ul><ul><li>minimum-cost tour, with cost 7. </li></ul>

×