1 / 66

Advanced Graph Modelling and Searching

Advanced Graph Modelling and Searching. HKOI Training 2008. Graph. A graph is a set of vertices and a set of edges G = (V, E) Number of vertices = |V| Number of edges = |E| We assume simple graph, so |E| = O(|V| 2 ). Trees in graph theory.

steffi
Download Presentation

Advanced Graph Modelling and Searching

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. Advanced Graph Modelling and Searching HKOI Training 2008

  2. Graph • A graph is a set of vertices and a set of edges • G = (V, E) • Number of vertices = |V| • Number of edges = |E| • We assume simple graph, so |E| = O(|V|2)

  3. Trees in graph theory • In graph theory, a tree is an acyclic, connected graph • Acyclic means “without cycles”

  4. Properties of trees • |E| = |V| - 1 • |E| = (|V|) • Between any pair of vertices, there is a unique path • Adding an edge between a pair of non-adjacent vertices creates exactly one cycle • Removing an edge from the tree breaks the tree into two smaller trees

  5. Definition? • The following four conditions are equivalent: • G is connected and acyclic • G is connected and |E| = |V| - 1 • G is acyclic and |E| = |V| - 1 • Between any pair of vertices in G, there exists a unique path • G is a tree if at least one of the above conditions is satisfied

  6. ancestors root parent siblings descendants children Trees and related terms

  7. Representation of Graph • Adjacency Matrix • Adjacency list • Edge list

  8. Representation of Graph

  9. Graph Traversal • Given: a graph • Goal: visit all (or some) vertices and edges of the graph using some strategy (the order of visit is systematic) • DFS, BFS are examples of graph traversal algorithms • Some shortest path algorithms and spanning tree algorithms have specific visit order

  10. Idea of DFS and BFS • This is a brief idea of DFS and BFS • DFS: continue visiting next vertex whenever there is a road, go back if no road (ie. visit to the depth of current path) • Example: a human want to visit a place, but do not know the path • BFS: go through all the adjacent vertices before going further (ie. spread among next vertices) • Example: set a house on fire, the fire will spread through the house

  11. DFS (pseudo code) DFS (vertex u) { mark u as visited for each vertex v directly reachable from u if v is unvisited DFS (v) } • Initially all vertices are marked as unvisited

  12. F E unvisited visited A D B C DFS (Demonstration)

  13. “Advanced” DFS • Apart from just visiting the vertices, DFS can also provide us with valuable information • DFS can be enhanced by introducing: • birth time and death time of a vertex • birth time: when the vertex is first visited • death time: when we retreat from the vertex • DFS tree • parent of a vertex

  14. DFS spanning tree / forest • A rooted tree • The root is the start vertex • If v is first visited from u, then u is the parent of v in the DFS tree • Edges are those in forward direction of DFS, ie. when visiting vertices that are not visited before • If some vertices are not reachable from the start vertex, those vertices will form other spanning trees (1 or more) • The collection of the trees are called forest

  15. DFS (pseudo code) DFS (vertex u) { mark u as visited time  time+1; birth[u]=time; for each vertex v directly reachable from u if v is unvisited parent[v]=u DFS (v) time  time+1; death[u]=time; }

  16. A D B E G E F C A D H F unvisited B visited visited (dead) C G H DFS forest (Demonstration) 1 2 3 13 10 4 14 6 12 9 8 16 11 5 15 7 - A B - A C D C

  17. A D B E G C F H Classification of edges • Tree edge • Forward edge • Back edge • Cross edge • Question: which type of edges is always absent in an undirected graph?

  18. Determination of edge types • How to determine the type of an arbitrary edge (u, v) after DFS? • Tree edge • parent [v] = u • Forward edge • not a tree edge; and • birth [v] > birth [u]; and • death [v] < death [u] • How about back edge and cross edge?

  19. Determination of edge types

  20. Applications of DFS Forests • Topological sorting (Tsort) • Strongly-connected components (SCC) • Some more “advanced” algorithms

  21. Example: Tsort • Topological order: A numbering of the vertices of a directed acyclic graph such that every edge from a vertex numbered i to a vertex numbered j satisfies i<j • Tsort: Number the vertices in topological order 3 6 1 7 2 5 4

  22. Tsort Algorithm • If the graph has more then one vertex that has indegree 0, add a vertice to connect to all indegree-0 vertices • Let the indegree 0 vertice be s • Use s as start vertice, and compute the DFS forest • The death time of the vertices represent the reverse of topological order

  23. Tsort (Demonstration) 1 2 3 4 12 13 8 5 16 11 10 7 15 14 9 6 A B C G S D F E G C F B A E D  D E A B F C G

  24. Example: SCC • A graph is strongly-connected if • for any pair of vertices u and v, one can go from u to v and from v to u. • Informally speaking, an SCC of a graph is a subset of vertices that • forms a strongly-connected subgraph • does not form a strongly-connected subgraph with the addition of any new vertex

  25. SCC (Illustration)

  26. SCC (Algorithm) • Compute the DFS forest of the graph G to get the death time of the vertices • Reverse all edges in G to form G’ • Compute a DFS forest of G’, but always choose the vertex with the latest death time when choosing the root for a new tree • The SCCs of G are the DFS trees in the DFS forest of G’

  27. F A D B C G H SCC (Demonstration) 1 2 3 13 10 4 14 6 12 9 8 16 11 5 15 7 - A B - A C D C E F A E B H D A D G F B C C G H

  28. A E B H E D F G F C A D B C G H SCC (Demonstration)

  29. DFS Summary • DFS spanning tree / forest • We can use birth time and death time in DFS spanning tree to do varies things, such as Tsort, SCC • Notice that in the previous slides, we related birth time and death time. But in the discussed applications, birth time and death time can be independent, ie. birth time and death time can use different time counter

  30. Breadth-first search (BFS) • In order to “spread”, we need to makes use of a data structure, queue ,to remember just visited vertices • Revised: • DFS: continue visiting next vertex whenever there is a road, go back if no road (ie. visit to the depth of current path) • BFS: go through all the adjacent vertices before going further (ie. spread among next vertices)

  31. BFS (Pseudo code) while queue not empty dequeue the first vertex u from queue for each vertex v directly reachable from u if v is unvisited enqueue v to queue mark v as visited • Initially all vertices except the start vertex are marked as unvisited and the queue contains the start vertex only

  32. I G D C H unvisited visited A E J visited (dequeued) F B BFS (Demonstration) Queue: A B C F D E H G J I

  33. Applications of BFS • Shortest paths finding • Flood-fill (can also be handled by DFS)

  34. Comparisons of DFS and BFS

  35. What is graph modeling? • Conversion of a problem into a graph problem • Sometimes a problem can be easily solved once its underlying graph model is recognized • Graph modeling appears almost every year in NOI or IOI

  36. Basics of graph modeling • A few steps: • identify the vertices and the edges • identify the objective of the problem • state the objective in graph terms • implementation: • construct the graph from the input instance • run the suitable graph algorithms on the graph • convert the output to the required format

  37. start goal Simple examples (1) • Given a grid maze with obstacles, find a shortest path between two given points

  38. Simple examples (2) • A student has the phone numbers of some other students • Suppose you know all pairs (A, B) such that A has B’s number • Now you want to know Alan’s number, what is the minimum number of calls you need to make?

  39. Simple examples (2) • Vertex: student • Edge: whether A has B’s number • Add an edge from A to B if A has B’s number • Problem: find a shortest path from your vertex to Alan’s vertex

  40. Complex examples (1) • Same settings as simple example 1 • You know a trick – walking through an obstacle! However, it can be used for only once • What should a vertex represent? • your position only? • your position + whether you have used the trick

  41. Complex examples (1) • A vertex is in the form (position, used) • The vertices are divided into two groups • trick used • trick not used

  42. start goal Complex examples (1) unused start goal used goal

  43. Complex examples (1) • How about you can walk through obstacles for k times?

  44. Complex examples (1) k start goal k-1 k-2

  45. Complex examples (1) k start goal k-1 k-4 k-3 k-2

  46. 1 2 3 4 5 6 7 8 Complex examples (2) • The famous 8-puzzle • Given a state, find the moves that bring it to the goal state

  47. Complex examples (2) • What does a vertex represent? • the position of the empty square? • the number of tiles that are in wrong positions? • the state (the positions of the eight tiles) • What are the edges? • What is the equivalent graph problem?

  48. 1 2 3 4 5 6 1 2 3 7 8 4 5 6 7 8 1 2 3 4 6 7 5 8 1 1 1 2 2 2 3 3 4 4 4 5 5 3 5 7 7 7 8 8 8 6 6 6 1 2 3 4 5 6 7 8 Complex examples (2)

  49. Complex examples (3) • Theseus and Minotaur • http://www.logicmazes.com/theseus.html • Extract: • Theseus must escape from a maze. There is also a mechanical Minotaur in the maze. For every turn that Theseus takes, the Minotaur takes two turns. The Minotaur follows this program for each of his two turns: • First he tests if he can move horizontally and get closer to Theseus. If he can, he will move one square horizontally. If he can’t, he will test if he could move vertically and get closer to Theseus. If he can, he will move one square vertically. If he can’t move either horizontally or vertically, then he just skips that turn.

  50. Complex examples (3) • What does a vertex represent? • Theseus’ position • Minotaur’s position • Both

More Related