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)?

Graps 2

  • 2.
  • 3.
    Time Complexity ofBFS(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 RecordingBFSwe 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 + PathFinding 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 fromTrysome 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 pathsfound 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 werecord the shortest distances?d(v) = ;d(s) = 0;d(w)=d(v)+1;
  • 22.
    Application of BFSOne 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.
  • 23.
  • 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 isconnectedifand 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)
  • 26.
  • 27.
  • 28.
    Connected ComponentsFormally stated:Aconnected component is a maximal connected subgraph of a graph.The set of connected components is unique for a given graph.
  • 29.
    Finding Connected ComponentsForeach vertexIf not visitedCall DFSThis will findall verticesconnectedto “v” => oneconnected componentBasic DFS algorithm.
  • 30.
    Time ComplexityRunning timefor each i connected componentQuestion:Can two connected components have the same edge?Can two connected components have the same vertex?So:
  • 31.
    TreesTree arises inmany 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 graphis 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 matrixand adjacency list can be used
  • 35.
    Directed Acyclic GraphAdirected 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 OutdegreeSince 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 thearcs going “out” from vSimple to computeScan through list Adj[v] and count the arcsWhat is the total outdegree? (m=#edges)
  • 38.
    IndegreeAll of thearcs 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 + OutdegreeEacharc (u,v) contributes count 1 to the outdegree of u and count 1 to the indegree of v.
  • 40.
  • 41.
    Directed Graphs UsageDirectedgraphs 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 departmentscourse structure Any directed cycles?104180171151221342252211251271M132M111201231272361381303343341327334336362332How many indeg(171)?How many outdeg(171)?