SlideShare uma empresa Scribd logo
1 de 42
Graph
Time Complexity of BFS(Using adjacency matrix) Assume adjacency list n = number of vertices   m = number of edges O(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 Recording BFS 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 -1 Record where  you came from
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred Initialize visited table (all False) Initialize Pred to -1 {    } Q =  Initialize Q to be empty
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred Flag that 2 has  been visited. {  2   } Q =  Place source 2 on the queue.
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred Mark neighbors as visited. Record in Pred that we came from 2. {2} ->  {  8, 1, 4 } Q =  Dequeue 2.   Place all unvisited neighbors of 2 on the queue
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred Mark new visited Neighbors. Record in Pred that 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!
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred Mark new visited Neighbors. Record in Pred that 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.
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred { 4, 0, 9, 3, 7 } -> { 0, 9, 3, 7 }  Q =  Dequeue 4.    -- 4 has no unvisited neighbors!
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred { 0, 9, 3, 7 } -> { 9, 3, 7 }  Q =  Dequeue 0.    -- 0 has no unvisited neighbors!
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred { 9, 3, 7 } -> { 3, 7 }  Q =  Dequeue 9.    -- 9 has no unvisited neighbors!
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred Mark new visited Vertex 5. Record in Pred that we came from 3. { 3, 7 } -> { 7, 5 }  Q =  Dequeue 3.    -- place neighbor 5 on the queue.
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred Mark new visited Vertex 6. Record in Pred that we came from 7. { 7, 5 } -> { 5, 6 }  Q =  Dequeue 7.    -- place neighbor 6 on the queue.
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred { 5, 6} -> { 6 }  Q =  Dequeue 5.    -- no unvisited neighbors of 5.
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred { 6 } -> {  }  Q =  Dequeue 6.    -- no unvisited neighbors of 6.
0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred Pred now can be traced backward to report the path! {  }  STOP!!!   Q is empty!!! Q =
Path reporting nodes visited from Try 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 tree The 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: Connectivity G = P Q N L R O M s D E C A How do we tell if two vertices  are connected? F B K G A connected to F? A connected to L? H
Connectivity A 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 Components Formally 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 Components For each vertex If not visited Call DFS This will find all vertices connected to “v” => one connected component Basic DFS algorithm.
Time Complexity Running time for each i connected component Question: Can two connected components have the same edge? Can two connected components have the same vertex? So:
Trees Tree arises in many computer science applications A 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 equivalent G is a tree G is acyclic and has exactly n-1 edges G is connected and has exactly n-1 edges
Tree as a (directed) Graph Is it a graph? Does it contain cycles?    (in other words,  is it acyclic) How many vertices? How many edges? 15 6 18 8 3 30 16
Directed Graph A 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 graph An edge is denoted {u,v}, which actually corresponds to two arcs (u,v) and (v,u)
Representations The adjacency matrix and adjacency list can be used
Directed Acyclic Graph A directed path is a sequence of vertices (v0, v1, . . . , vk) Such that (vi, vi+1) is an arc A 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 directed We can’t simply talk about Deg(v) Instead, we need to consider the arcs coming  “in” and going “out” Thus, we define terms Indegree(v) Outdegree(v)
Outdegree All of the arcs going “out” from v Simple to compute Scan through list Adj[v] and count the arcs What is the total outdegree? (m=#edges)
Indegree All of the arcs coming “in” to v Not as simple to compute as outdegree First, initialize indegree[v]=0 for each vertex v Scan through adj[v] list for each v For each vertex w seen, indegree[w]++; Running time: O(n+m) What is the total indegree?
Indegree + Outdegree Each arc (u,v) contributes count 1 to the outdegree of u and count 1 to the indegree of v.
Example 3 6 8 0 7 2 9 1 5 4 Indeg(2)? Indeg(8)? Outdeg(0)? Num of Edges? Total OutDeg? Total Indeg?
Directed Graphs Usage Directed graphs are often used to represent order-dependent tasks That is we cannot start a task before another task finishes We can model this task dependent constraint using arcs An arc (i,j) means task j cannot start until task i is finished Clearly, for the system not to hang, the graph must be acyclic.  j i Task j cannot start until task i is finished
University Example CS departments course structure  Any directed cycles? 104 180 171 151 221 342 252 211 251 271 M132 M111 201 231 272 361 381 303 343 341 327 334 336 362 332 How many indeg(171)? How many outdeg(171)?

Mais conteúdo relacionado

Mais procurados

Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graphgetacew
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsSigSegVSquad
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsYi-Lung Tsai
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmMSA Technosoft
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph AJAL A J
 
Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Traian Rebedea
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)Ishucs
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first searchHossain Md Shakhawat
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data StructureProf Ansari
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs applicationUmme habiba
 

Mais procurados (20)

Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graph
 
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12
 
BFS
BFSBFS
BFS
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Data structure
Data structureData structure
Data structure
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Graphss
GraphssGraphss
Graphss
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 

Destaque

Jet centrifugal multistage
Jet  centrifugal multistageJet  centrifugal multistage
Jet centrifugal multistageEko Kiswanto
 
Fa whizol poster_a2
Fa whizol poster_a2Fa whizol poster_a2
Fa whizol poster_a2Eko Kiswanto
 
130524 pf nc5_plus_en_media_01
130524 pf nc5_plus_en_media_01130524 pf nc5_plus_en_media_01
130524 pf nc5_plus_en_media_01Muhammad Ahmad
 
Mesin Diesel Toyota tipe 2 KD FTV 14
Mesin Diesel Toyota tipe  2 KD FTV 14Mesin Diesel Toyota tipe  2 KD FTV 14
Mesin Diesel Toyota tipe 2 KD FTV 14Eko Kiswanto
 

Destaque (7)

Jet centrifugal multistage
Jet  centrifugal multistageJet  centrifugal multistage
Jet centrifugal multistage
 
Fa whizol poster_a2
Fa whizol poster_a2Fa whizol poster_a2
Fa whizol poster_a2
 
Ppk jamsostek
Ppk jamsostekPpk jamsostek
Ppk jamsostek
 
Jet submersible
Jet submersibleJet submersible
Jet submersible
 
130524 pf nc5_plus_en_media_01
130524 pf nc5_plus_en_media_01130524 pf nc5_plus_en_media_01
130524 pf nc5_plus_en_media_01
 
0901b8038023408c
0901b8038023408c0901b8038023408c
0901b8038023408c
 
Mesin Diesel Toyota tipe 2 KD FTV 14
Mesin Diesel Toyota tipe  2 KD FTV 14Mesin Diesel Toyota tipe  2 KD FTV 14
Mesin Diesel Toyota tipe 2 KD FTV 14
 

Semelhante a Graps 2

Week12 graph
Week12   graph Week12   graph
Week12 graph IIUM
 
lecture 17
lecture 17lecture 17
lecture 17sajinsc
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxwhittemorelucilla
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Traian Rebedea
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithmjyothimonc
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxrandyburney60861
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programMuhammad Danish Badar
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm Ejeniihykdevara
 

Semelhante a Graps 2 (20)

Graphs
GraphsGraphs
Graphs
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 
lecture 17
lecture 17lecture 17
lecture 17
 
Graphs
GraphsGraphs
Graphs
 
Graph
GraphGraph
Graph
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
 
10.graph
10.graph10.graph
10.graph
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
Graphs
GraphsGraphs
Graphs
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
 

Mais de Saurabh Mishra (7)

Sorting2
Sorting2Sorting2
Sorting2
 
Sorting
SortingSorting
Sorting
 
Searching
SearchingSearching
Searching
 
Presentation1
Presentation1Presentation1
Presentation1
 
Data structures
Data structuresData structures
Data structures
 
Trees
TreesTrees
Trees
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 

Graps 2

  • 1.
  • 3. Time Complexity of BFS(Using adjacency matrix) Assume adjacency list n = number of vertices m = number of edges O(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 Recording BFS 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 -1 Record where you came from
  • 6. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred Initialize visited table (all False) Initialize Pred to -1 { } Q = Initialize Q to be empty
  • 7. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred Flag that 2 has been visited. { 2 } Q = Place source 2 on the queue.
  • 8. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred Mark neighbors as visited. Record in Pred that we came from 2. {2} -> { 8, 1, 4 } Q = Dequeue 2. Place all unvisited neighbors of 2 on the queue
  • 9. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred Mark new visited Neighbors. Record in Pred that 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. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred Mark new visited Neighbors. Record in Pred that 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. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred { 4, 0, 9, 3, 7 } -> { 0, 9, 3, 7 } Q = Dequeue 4. -- 4 has no unvisited neighbors!
  • 12. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred { 0, 9, 3, 7 } -> { 9, 3, 7 } Q = Dequeue 0. -- 0 has no unvisited neighbors!
  • 13. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred { 9, 3, 7 } -> { 3, 7 } Q = Dequeue 9. -- 9 has no unvisited neighbors!
  • 14. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) Neighbors source Pred Mark new visited Vertex 5. Record in Pred that we came from 3. { 3, 7 } -> { 7, 5 } Q = Dequeue 3. -- place neighbor 5 on the queue.
  • 15. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred Mark new visited Vertex 6. Record in Pred that we came from 7. { 7, 5 } -> { 5, 6 } Q = Dequeue 7. -- place neighbor 6 on the queue.
  • 16. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred { 5, 6} -> { 6 } Q = Dequeue 5. -- no unvisited neighbors of 5.
  • 17. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Neighbors Pred { 6 } -> { } Q = Dequeue 6. -- no unvisited neighbors of 6.
  • 18. 0 8 2 9 1 7 3 6 4 5 Example Adjacency List Visited Table (T/F) source Pred Pred now can be traced backward to report the path! { } STOP!!! Q is empty!!! Q =
  • 19. Path reporting nodes visited from Try 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 tree The 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: Connectivity G = P Q N L R O M s D E C A How do we tell if two vertices are connected? F B K G A connected to F? A connected to L? H
  • 25. Connectivity A 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 Components Formally 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 Components For each vertex If not visited Call DFS This will find all vertices connected to “v” => one connected component Basic DFS algorithm.
  • 30. Time Complexity Running time for each i connected component Question: Can two connected components have the same edge? Can two connected components have the same vertex? So:
  • 31. Trees Tree arises in many computer science applications A 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 equivalent G is a tree G is acyclic and has exactly n-1 edges G is connected and has exactly n-1 edges
  • 32. Tree as a (directed) Graph Is it a graph? Does it contain cycles? (in other words, is it acyclic) How many vertices? How many edges? 15 6 18 8 3 30 16
  • 33. Directed Graph A 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 graph An edge is denoted {u,v}, which actually corresponds to two arcs (u,v) and (v,u)
  • 34. Representations The adjacency matrix and adjacency list can be used
  • 35. Directed Acyclic Graph A directed path is a sequence of vertices (v0, v1, . . . , vk) Such that (vi, vi+1) is an arc A 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 directed We can’t simply talk about Deg(v) Instead, we need to consider the arcs coming “in” and going “out” Thus, we define terms Indegree(v) Outdegree(v)
  • 37. Outdegree All of the arcs going “out” from v Simple to compute Scan through list Adj[v] and count the arcs What is the total outdegree? (m=#edges)
  • 38. Indegree All of the arcs coming “in” to v Not as simple to compute as outdegree First, initialize indegree[v]=0 for each vertex v Scan through adj[v] list for each v For each vertex w seen, indegree[w]++; Running time: O(n+m) What is the total indegree?
  • 39. Indegree + Outdegree Each arc (u,v) contributes count 1 to the outdegree of u and count 1 to the indegree of v.
  • 40. Example 3 6 8 0 7 2 9 1 5 4 Indeg(2)? Indeg(8)? Outdeg(0)? Num of Edges? Total OutDeg? Total Indeg?
  • 41. Directed Graphs Usage Directed graphs are often used to represent order-dependent tasks That is we cannot start a task before another task finishes We can model this task dependent constraint using arcs An arc (i,j) means task j cannot start until task i is finished Clearly, for the system not to hang, the graph must be acyclic. j i Task j cannot start until task i is finished
  • 42. University Example CS departments course structure Any directed cycles? 104 180 171 151 221 342 252 211 251 271 M132 M111 201 231 272 361 381 303 343 341 327 334 336 362 332 How many indeg(171)? How many outdeg(171)?