1 / 57

Data Structure & Algorithm

Data Structure & Algorithm. 10 – Graph & BFS & DFS JJCAO. Graph Are Not. Graphs. G = (V,E ) V[G] = {1,2,3,4,5,6} |V| = 6 E[G] = {{1,2},{1,5},{2,5},{3,6}} Note: { u,v } = ( u,v ) = ( v,u ) ( u,v ): u ↔ v. Can be very complex. Excellent Tool!. Terminology.

jarah
Télécharger la présentation

Data Structure & Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure & Algorithm 10 – Graph & BFS & DFS JJCAO

  2. Graph Are Not

  3. Graphs • G = (V,E) • V[G] = {1,2,3,4,5,6} |V| = 6 • E[G] = {{1,2},{1,5},{2,5},{3,6}} • Note: {u,v} = (u,v) = (v,u) • (u,v): u↔v

  4. Can be very complex. Excellent Tool!

  5. Terminology

  6. Adjacency ListRepresentation

  7. Adjacency MatrixRepresentation

  8. Object-OrientedRepresentation • Node: some structure, with all relevant information • Edge: name & pointers to two endpoints

  9. Sub-Graphs

  10. More Terminology

  11. Connectivity

  12. Connected Components • Every node v is connected to itself • if u and v are in the same connected component then v is connected to u and u is connected to v • Connected components form a partition of the nodes and so are disjoint:

  13. Forests & Trees

  14. Properties

  15. Directed Graph

  16. Adding Direction • An undirected graph can be transformed into a directed one

  17. Terminology • endpoints of an edge = the vertices it connects • e = (u,v) is incident from u = leaves u incident to v = enters v • u is the tail • v is the head • degreeof v = indegree + outdegree • indegree= # incoming edges • outdegree= # outgoing edges • Paths and cycles are now directed

  18. Simple Graphs Are graphs with no parallel edges, no self loops Properties:

  19. More Properties

  20. Strong Connectivity • Vertices strongly connected ↔ there is a path from each to the other • Graph Strongly connected↔every2 vertices are strongly connected • Strongly Connected Components = maximal strongly connected subgraphs

  21. Strong Connectivity The strongly connected components of the above graph

  22. Graph Traversals Given: a graph G a source vertex s in V[G] Goal: “visit” all the vertices of G to determine some property: – Is G connected? – Does G contain a cycle? – is there a v u path in G? – What is the shortest path connecting v and u? – Will G disconnect if we remove a single edge? A vertex? – G is the WWW - follow links to retrieve information….

  23. Data Structure for BFS • First In First Out (FIFO) Queue

  24. Breadth First Search

  25. Example

  26. BFS - Running Time Adjacency list representation:

  27. Shortest Paths Goal: Find a shortest path from s to every other v Idea: - start at s - find all vertices at distance 1 - find all vertices at distance 2

  28. BFS for Shortest Paths

  29. Shortest-Paths Claim

  30. BFS for Connectivity

  31. Depth First Search Goal: Visit all vertices of the graph Idea: - start at s - keep going “deeper” into the graph whenever possible

  32. Depth First Search

  33. Data Structure for DFS • Last In First Out (LIFO) Stack Rewrite the procedure DFS, using a stack to eliminate recursion

  34. Example

  35. Time Stamping

  36. Example

  37. BFS vs. DFS

  38. Classification of Edges in DFS Forest Relative to the same DFS tree

  39. Classification of Edges

  40. DFS - Running Time Adjacency list representation

  41. Applications of DFS In (V+E) time, we can: • Find connected components of G • determine if G has a cycle • determine if removing an edge / vertex disconnects G • determine if G is planar • …

  42. Cycles in Directed Graphs Theorem: DiGraphG has a cycle DFS forest has a back edge Proof: • back edge => cycle: obvious • cycle => back edge: Lemma: A directed graph G is acyclicif and only if a depth-first search of G yields no back edges.

  43. Topological Sort an ordering "<" of V[G] such that if (u, v) E[G] => u < v

  44. Application of Topological Sort Many applications use directed acyclic graphs to indicate precedence among events, such as Professor Bumsteadgets dressed in the morning.

  45. Topological Sort Lemma: G can be topologically sorted <=> G is a DAG (directed acyclic graph) Proof: cycle => G can't be sorted - obvious: no cycle => G can be sorted: we will show an algorithm

  46. A Simple TopSort Algorithm Source = a vertex with indegree= 0

  47. A Simple TopSort Algorithm Source = a vertex with indegree= 0

  48. Adjacency MatrixImplementation

More Related