SlideShare a Scribd company logo
Graps 2
Graph
Time Complexity of BFS(Using adjacency matrix)Assume adjacency listn = number of vertices   m = number of edgesO(n2)Finding the adjacent vertices of v requires checking all elements in the row. This takes linear time O(n).Summing over all the n iterations, the total running time is O(n2).So, with adjacency matrix, BFS is O(n2) independent of number of edges m.  With adjacent lists, BFS is O(n+m); if m=O(n2) like a dense graph, O(n+m)=O(n2).
Shortest Path RecordingBFS we saw only tells us whether a path exists from source s, to other vertices v.It doesn’t tell us the path!We need to modify the algorithm to record the path.How can we do that?Note: we do not know which vertices lie on this path until we reach v!Efficient solution:Use an additional array pred[0..n-1]Pred[w] = v  means that vertex w was visited from  v
BFS + Path Finding initialize all pred[v] to -1Record where  you came from
0829173645ExampleAdjacency ListVisited Table (T/F)sourcePredInitialize visitedtable (all False)Initialize Pred to -1{    }Q = Initialize Q to be empty
0829173645ExampleAdjacency ListVisited Table (T/F)sourcePredFlag that 2 has been visited.{  2   }Q = Place source 2 on the queue.
0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePredMark neighborsas visited.Record in Predthat we came from 2.{2} ->  {  8, 1, 4 }Q = Dequeue 2.  Place all unvisited neighbors of 2 on the queue
0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPredMark new visitedNeighbors.Record in Predthat we came from 8.{  8, 1, 4 } -> { 1, 4, 0, 9 } Q = Dequeue 8.   -- Place all unvisited neighbors of 8 on the queue. -- Notice that 2 is not placed on the queue again, it has been visited!
0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePredMark new visitedNeighbors.Record in Predthat we came from 1.{  1, 4, 0, 9 } -> { 4, 0, 9, 3, 7 } Q = Dequeue 1.   -- Place all unvisited neighbors of 1 on the queue. -- Only nodes 3 and 7 haven’t been visited yet.
0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePred{ 4, 0, 9, 3, 7 } -> { 0, 9, 3, 7 } Q = Dequeue 4.   -- 4 has no unvisited neighbors!
0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePred{ 0, 9, 3, 7 } -> { 9, 3, 7 } Q = Dequeue 0.   -- 0 has no unvisited neighbors!
0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPred{ 9, 3, 7 } -> { 3, 7 } Q = Dequeue 9.   -- 9 has no unvisited neighbors!
0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePredMark new visitedVertex 5.Record in Predthat we came from 3.{ 3, 7 } -> { 7, 5 } Q = Dequeue 3.   -- place neighbor 5 on the queue.
0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPredMark new visitedVertex 6.Record in Predthat we came from 7.{ 7, 5 } -> { 5, 6 } Q = Dequeue 7.   -- place neighbor 6 on the queue.
0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPred{ 5, 6} -> { 6 } Q = Dequeue 5.   -- no unvisited neighbors of 5.
0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPred{ 6 } -> {  } Q = Dequeue 6.   -- no unvisited neighbors of 6.
0829173645ExampleAdjacency ListVisited Table (T/F)sourcePredPred now can be traced backwardto report the path!{  } STOP!!!   Q is empty!!!Q =
Path reportingnodesvisited fromTry some examples, report path from s to v:Path(0) ->Path(6) ->Path(1) ->The path returned is the shortest from s to v (minimum number of edges).
BFS treeThe paths found by BFS is often drawn as a rooted tree (called BFS tree), with the starting vertex as the root of the tree.BFS tree for vertex s=2.Question: What would a “level” order traversal tell you?
How do we record the shortest distances?d(v) = ;d(s) = 0;d(w)=d(v)+1;
Application of BFS One application concerns how to find        connected components in a graph If a graph has more than one connected components, BFS builds a BFS-forest (not just BFS-tree)!Each tree in the forest is a connected component.
Connected Components,Directed graphs,Topological sort
Application 1: ConnectivityG =PQNLROMsDECAHow do we tell if two vertices are connected?FBKGA connected to F?A connected to L?H
ConnectivityA graph isconnectedif and only if there exists a path between every pair of distinct vertices.A graph is connected if and only if there exists a simple path between every pair of distinct vertices (since every non-simple path contains a cycle, which can be bypassed)How to check for connectivity?Run BFS or DFS (using an arbitrary vertex as the source)If all vertices have been visited, the graph is connected.Running time?  O(n + m)
Connected Components
Subgraphs
Connected ComponentsFormally stated:A connected component is a maximal connected subgraph of a graph.The set of connected components is unique for a given graph.
Finding Connected ComponentsFor each vertexIf not visitedCall DFSThis will findall verticesconnectedto “v” => oneconnected componentBasic DFS algorithm.
Time ComplexityRunning time for each i connected componentQuestion:Can two connected components have the same edge?Can two connected components have the same vertex?So:
TreesTree arises in many computer science applicationsA graph G is a tree if and only if it is connected and acyclic  (Acyclic means it does not contain any simple cycles)The following statements are equivalentG is a treeG is acyclic and has exactly n-1 edgesG is connected and has exactly n-1 edges
Tree as a (directed) GraphIs it a graph?Does it contain cycles?   (in other words,  is it acyclic)How many vertices?How many edges?15618833016
Directed GraphA graph is directed if direction is assigned to each edge.  We call the directed edges arcs.An edge is denoted as an ordered pair (u, v) Recall: for an undirected graphAn edge is denoted {u,v}, which actually corresponds to two arcs (u,v) and (v,u)
RepresentationsThe adjacency matrix and adjacency list can be used
Directed Acyclic GraphA directed path is a sequence of vertices (v0, v1, . . . , vk)Such that (vi, vi+1) is an arcA directed cycleis a directed path such that the first and last vertices are the same.A directed graph is acyclic if it does not contain any directed cycles
Indegree and Outdegree Since the edges are directedWe can’t simply talk about Deg(v)Instead, we need to consider the arcs coming  “in” and going “out”Thus, we define termsIndegree(v)Outdegree(v)
OutdegreeAll of the arcs going “out” from vSimple to computeScan through list Adj[v] and count the arcsWhat is the total outdegree? (m=#edges)
IndegreeAll of the arcs coming “in” to vNot as simple to compute as outdegreeFirst, initialize indegree[v]=0 for each vertex vScan through adj[v] list for each vFor each vertex w seen, indegree[w]++;Running time: O(n+m)What is the total indegree?
Indegree + OutdegreeEach arc (u,v) contributes count 1 to the outdegree of u and count 1 to the indegree of v.
Example3680729154Indeg(2)?Indeg(8)?Outdeg(0)?Num of Edges?Total OutDeg?Total Indeg?
Directed Graphs UsageDirected graphs are often used to represent order-dependent tasksThat is we cannot start a task before another task finishesWe can model this task dependent constraint using arcsAn arc (i,j) means task j cannot start until task i is finishedClearly, for the system not to hang, the graph must be acyclic. jiTask j cannot start until task i is finished
University ExampleCS departments course structure Any directed cycles?104180171151221342252211251271M132M111201231272361381303343341327334336362332How many indeg(171)?How many outdeg(171)?

More Related Content

PPTX
Topological sort
jabishah
 
PPT
Bfs and dfs in data structure
Ankit Kumar Singh
 
PDF
Skiena algorithm 2007 lecture11 breadth deapth first search
zukun
 
PPTX
Unit 9 graph
Dabbal Singh Mahara
 
PDF
Topological Sort
Dr Sandeep Kumar Poonia
 
PPTX
Graph Algorithms: Breadth-First Search (BFS)
Md. Shafiuzzaman Hira
 
PPTX
130210107039 2130702
Ketaki_Pattani
 
PPTX
Unit ix graph
Tribhuvan University
 
Topological sort
jabishah
 
Bfs and dfs in data structure
Ankit Kumar Singh
 
Skiena algorithm 2007 lecture11 breadth deapth first search
zukun
 
Unit 9 graph
Dabbal Singh Mahara
 
Topological Sort
Dr Sandeep Kumar Poonia
 
Graph Algorithms: Breadth-First Search (BFS)
Md. Shafiuzzaman Hira
 
130210107039 2130702
Ketaki_Pattani
 
Unit ix graph
Tribhuvan University
 

What's hot (20)

PDF
Algorithms of graph
getacew
 
PDF
Analysis of Pathfinding Algorithms
SigSegVSquad
 
PDF
Problem Solving with Algorithms and Data Structure - Graphs
Yi-Lung Tsai
 
PPTX
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
DOC
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
PPT
Directed Acyclic Graph
AJAL A J
 
PPT
Breadth first search
Sazzad Hossain
 
PPT
Graphs bfs dfs
Jaya Gautam
 
PDF
Algorithm Design and Complexity - Course 12
Traian Rebedea
 
PPTX
BFS
jyothimonc
 
PPTX
Breadth first search (Bfs)
Ishucs
 
PDF
Bfs dfs
Amit Kumar Rathi
 
PPTX
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
PPT
Data structure
sumit singh
 
PPT
Breadth first search and depth first search
Hossain Md Shakhawat
 
PDF
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
PPTX
Graphss
fika sweety
 
PPT
2.5 graph dfs
Krish_ver2
 
PDF
Graph in Data Structure
Prof Ansari
 
PPTX
Bfs & dfs application
Umme habiba
 
Algorithms of graph
getacew
 
Analysis of Pathfinding Algorithms
SigSegVSquad
 
Problem Solving with Algorithms and Data Structure - Graphs
Yi-Lung Tsai
 
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
Directed Acyclic Graph
AJAL A J
 
Breadth first search
Sazzad Hossain
 
Graphs bfs dfs
Jaya Gautam
 
Algorithm Design and Complexity - Course 12
Traian Rebedea
 
Breadth first search (Bfs)
Ishucs
 
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
Data structure
sumit singh
 
Breadth first search and depth first search
Hossain Md Shakhawat
 
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
Graphss
fika sweety
 
2.5 graph dfs
Krish_ver2
 
Graph in Data Structure
Prof Ansari
 
Bfs & dfs application
Umme habiba
 
Ad

Viewers also liked (7)

PDF
Jet centrifugal multistage
Eko Kiswanto
 
PDF
Fa whizol poster_a2
Eko Kiswanto
 
PDF
Ppk jamsostek
Eko Kiswanto
 
PDF
Jet submersible
Eko Kiswanto
 
PDF
130524 pf nc5_plus_en_media_01
Muhammad Ahmad
 
PDF
0901b8038023408c
Muhammad Ahmad
 
PPT
Mesin Diesel Toyota tipe 2 KD FTV 14
Eko Kiswanto
 
Jet centrifugal multistage
Eko Kiswanto
 
Fa whizol poster_a2
Eko Kiswanto
 
Ppk jamsostek
Eko Kiswanto
 
Jet submersible
Eko Kiswanto
 
130524 pf nc5_plus_en_media_01
Muhammad Ahmad
 
0901b8038023408c
Muhammad Ahmad
 
Mesin Diesel Toyota tipe 2 KD FTV 14
Eko Kiswanto
 
Ad

Similar to Graps 2 (20)

PPT
Graphs
Saurabh Mishra
 
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
sahadevbkbiet2023
 
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
sahadevbkbiet2023
 
PPT
14. GRAPH in data structures and algorithm.ppt
saurabhthege
 
PDF
Daa chpater 12
B.Kirron Reddi
 
PPTX
Graph Data Structure
Afaq Mansoor Khan
 
PPTX
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
PPTX
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
PDF
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
ChristianKapsales1
 
PPT
Chapter 23 aoa
Hanif Durad
 
PDF
Graph Data Structure
Keno benti
 
PPT
Unit VI - Graphs.ppt
HODElex
 
PPT
Graphs.ppt of mathemaics we have to clar all doubts
nc3186331
 
PPT
Graphs.ppt
lakshmi26355
 
PPT
Graphs concept in data structures and algorithms.ppt
vimalak8
 
PDF
Week12 graph
IIUM
 
PPTX
Data Structure and algorithms - Graph1.pptx
Kishor767966
 
PPTX
Basic Graph Algorithms Vertex (Node): lk
ymwjd5j8pb
 
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
sahadevbkbiet2023
 
Data structure Graph PPT ( BFS & DFS ) NOTES
sahadevbkbiet2023
 
14. GRAPH in data structures and algorithm.ppt
saurabhthege
 
Daa chpater 12
B.Kirron Reddi
 
Graph Data Structure
Afaq Mansoor Khan
 
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
ChristianKapsales1
 
Chapter 23 aoa
Hanif Durad
 
Graph Data Structure
Keno benti
 
Unit VI - Graphs.ppt
HODElex
 
Graphs.ppt of mathemaics we have to clar all doubts
nc3186331
 
Graphs.ppt
lakshmi26355
 
Graphs concept in data structures and algorithms.ppt
vimalak8
 
Week12 graph
IIUM
 
Data Structure and algorithms - Graph1.pptx
Kishor767966
 
Basic Graph Algorithms Vertex (Node): lk
ymwjd5j8pb
 

More from Saurabh Mishra (7)

PPTX
Sorting2
Saurabh Mishra
 
PPT
Sorting
Saurabh Mishra
 
PPT
Searching
Saurabh Mishra
 
PPTX
Presentation1
Saurabh Mishra
 
PPT
Data structures
Saurabh Mishra
 
PPT
Trees
Saurabh Mishra
 
PPTX
Binary trees1
Saurabh Mishra
 
Sorting2
Saurabh Mishra
 
Searching
Saurabh Mishra
 
Presentation1
Saurabh Mishra
 
Data structures
Saurabh Mishra
 
Binary trees1
Saurabh Mishra
 

Graps 2

  • 3. Time Complexity of BFS(Using adjacency matrix)Assume adjacency listn = number of vertices m = number of edgesO(n2)Finding the adjacent vertices of v requires checking all elements in the row. This takes linear time O(n).Summing over all the n iterations, the total running time is O(n2).So, with adjacency matrix, BFS is O(n2) independent of number of edges m. With adjacent lists, BFS is O(n+m); if m=O(n2) like a dense graph, O(n+m)=O(n2).
  • 4. Shortest Path RecordingBFS we saw only tells us whether a path exists from source s, to other vertices v.It doesn’t tell us the path!We need to modify the algorithm to record the path.How can we do that?Note: we do not know which vertices lie on this path until we reach v!Efficient solution:Use an additional array pred[0..n-1]Pred[w] = v means that vertex w was visited from v
  • 5. BFS + Path Finding initialize all pred[v] to -1Record where you came from
  • 6. 0829173645ExampleAdjacency ListVisited Table (T/F)sourcePredInitialize visitedtable (all False)Initialize Pred to -1{ }Q = Initialize Q to be empty
  • 7. 0829173645ExampleAdjacency ListVisited Table (T/F)sourcePredFlag that 2 has been visited.{ 2 }Q = Place source 2 on the queue.
  • 8. 0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePredMark neighborsas visited.Record in Predthat we came from 2.{2} -> { 8, 1, 4 }Q = Dequeue 2. Place all unvisited neighbors of 2 on the queue
  • 9. 0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPredMark new visitedNeighbors.Record in Predthat we came from 8.{ 8, 1, 4 } -> { 1, 4, 0, 9 } Q = Dequeue 8. -- Place all unvisited neighbors of 8 on the queue. -- Notice that 2 is not placed on the queue again, it has been visited!
  • 10. 0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePredMark new visitedNeighbors.Record in Predthat we came from 1.{ 1, 4, 0, 9 } -> { 4, 0, 9, 3, 7 } Q = Dequeue 1. -- Place all unvisited neighbors of 1 on the queue. -- Only nodes 3 and 7 haven’t been visited yet.
  • 11. 0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePred{ 4, 0, 9, 3, 7 } -> { 0, 9, 3, 7 } Q = Dequeue 4. -- 4 has no unvisited neighbors!
  • 12. 0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePred{ 0, 9, 3, 7 } -> { 9, 3, 7 } Q = Dequeue 0. -- 0 has no unvisited neighbors!
  • 13. 0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPred{ 9, 3, 7 } -> { 3, 7 } Q = Dequeue 9. -- 9 has no unvisited neighbors!
  • 14. 0829173645ExampleAdjacency ListVisited Table (T/F)NeighborssourcePredMark new visitedVertex 5.Record in Predthat we came from 3.{ 3, 7 } -> { 7, 5 } Q = Dequeue 3. -- place neighbor 5 on the queue.
  • 15. 0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPredMark new visitedVertex 6.Record in Predthat we came from 7.{ 7, 5 } -> { 5, 6 } Q = Dequeue 7. -- place neighbor 6 on the queue.
  • 16. 0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPred{ 5, 6} -> { 6 } Q = Dequeue 5. -- no unvisited neighbors of 5.
  • 17. 0829173645ExampleAdjacency ListVisited Table (T/F)sourceNeighborsPred{ 6 } -> { } Q = Dequeue 6. -- no unvisited neighbors of 6.
  • 18. 0829173645ExampleAdjacency ListVisited Table (T/F)sourcePredPred now can be traced backwardto report the path!{ } STOP!!! Q is empty!!!Q =
  • 19. Path reportingnodesvisited fromTry some examples, report path from s to v:Path(0) ->Path(6) ->Path(1) ->The path returned is the shortest from s to v (minimum number of edges).
  • 20. BFS treeThe paths found by BFS is often drawn as a rooted tree (called BFS tree), with the starting vertex as the root of the tree.BFS tree for vertex s=2.Question: What would a “level” order traversal tell you?
  • 21. How do we record the shortest distances?d(v) = ;d(s) = 0;d(w)=d(v)+1;
  • 22. Application of BFS One application concerns how to find connected components in a graph If a graph has more than one connected components, BFS builds a BFS-forest (not just BFS-tree)!Each tree in the forest is a connected component.
  • 24. Application 1: ConnectivityG =PQNLROMsDECAHow do we tell if two vertices are connected?FBKGA connected to F?A connected to L?H
  • 25. ConnectivityA graph isconnectedif and only if there exists a path between every pair of distinct vertices.A graph is connected if and only if there exists a simple path between every pair of distinct vertices (since every non-simple path contains a cycle, which can be bypassed)How to check for connectivity?Run BFS or DFS (using an arbitrary vertex as the source)If all vertices have been visited, the graph is connected.Running time? O(n + m)
  • 28. Connected ComponentsFormally stated:A connected component is a maximal connected subgraph of a graph.The set of connected components is unique for a given graph.
  • 29. Finding Connected ComponentsFor each vertexIf not visitedCall DFSThis will findall verticesconnectedto “v” => oneconnected componentBasic DFS algorithm.
  • 30. Time ComplexityRunning time for each i connected componentQuestion:Can two connected components have the same edge?Can two connected components have the same vertex?So:
  • 31. TreesTree arises in many computer science applicationsA graph G is a tree if and only if it is connected and acyclic (Acyclic means it does not contain any simple cycles)The following statements are equivalentG is a treeG is acyclic and has exactly n-1 edgesG is connected and has exactly n-1 edges
  • 32. Tree as a (directed) GraphIs it a graph?Does it contain cycles? (in other words, is it acyclic)How many vertices?How many edges?15618833016
  • 33. Directed GraphA graph is directed if direction is assigned to each edge. We call the directed edges arcs.An edge is denoted as an ordered pair (u, v) Recall: for an undirected graphAn edge is denoted {u,v}, which actually corresponds to two arcs (u,v) and (v,u)
  • 34. RepresentationsThe adjacency matrix and adjacency list can be used
  • 35. Directed Acyclic GraphA directed path is a sequence of vertices (v0, v1, . . . , vk)Such that (vi, vi+1) is an arcA directed cycleis a directed path such that the first and last vertices are the same.A directed graph is acyclic if it does not contain any directed cycles
  • 36. Indegree and Outdegree Since the edges are directedWe can’t simply talk about Deg(v)Instead, we need to consider the arcs coming “in” and going “out”Thus, we define termsIndegree(v)Outdegree(v)
  • 37. OutdegreeAll of the arcs going “out” from vSimple to computeScan through list Adj[v] and count the arcsWhat is the total outdegree? (m=#edges)
  • 38. IndegreeAll of the arcs coming “in” to vNot as simple to compute as outdegreeFirst, initialize indegree[v]=0 for each vertex vScan through adj[v] list for each vFor each vertex w seen, indegree[w]++;Running time: O(n+m)What is the total indegree?
  • 39. Indegree + OutdegreeEach arc (u,v) contributes count 1 to the outdegree of u and count 1 to the indegree of v.
  • 41. Directed Graphs UsageDirected graphs are often used to represent order-dependent tasksThat is we cannot start a task before another task finishesWe can model this task dependent constraint using arcsAn arc (i,j) means task j cannot start until task i is finishedClearly, for the system not to hang, the graph must be acyclic. jiTask j cannot start until task i is finished
  • 42. University ExampleCS departments course structure Any directed cycles?104180171151221342252211251271M132M111201231272361381303343341327334336362332How many indeg(171)?How many outdeg(171)?