introduction to graphs
play

Introduction to Graphs CS2110, Spring 2011 Cornell University A - PowerPoint PPT Presentation

Introduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships . Each graph is a set of nodes connected by edges . Synonym Graph Hostile Slick Icy Chilly Direct Nifty Cool Abrupt


  1. Introduction to Graphs CS2110, Spring 2011 Cornell University

  2. A graph is a data structure for representing relationships .

  3. Each graph is a set of nodes connected by edges .

  4. Synonym Graph Hostile Slick Icy Chilly Direct Nifty Cool Abrupt Sharp Composed

  5. Goals for Today ● Learn the formalisms behind graphs. ● Learn different representations for graphs. ● Learn about paths and cycles in graphs. ● See three ways of exploring a graph. ● Explore applications of graphs to real-world problems. ● Explore algorithms for drawing graphs.

  6. Formalisms ● A (directed) graph is a pair G = (V, E) where ● V are the vertices (nodes) of the graph. ● E are the edges (arcs) of the graph. ● Each edge is a pair (u, v) of the start and end (or source and sink ) of the edge.

  7. CAN MAN RAN MAT CAT SAT RAT

  8. Directed and Undirected Graphs ● A graph is directed if its edges specify which is the start and end node. ● Encodes asymmetric relationship. ● A graph is undirected if the edges don't distinguish between the start and end nodes. ● Encodes symmetric relationship. ● An undirected graph is a special case of a directed graph (just add edges both ways).

  9. How Big is a Graph G = (V, E)? ● Two measures: ● Number of vertices: |V| (often denoted n ) ● Number of edges: |E| (often denoted m ) ● |E| can be at most O(|V| 2 ) ● A graph is called sparse if it has few edges. A graph with many edges is called dense .

  10. Navigating a Graph B D A F C E

  11. Navigating a Graph B D A F C E

  12. Navigating a Graph B D A F C E A B D F

  13. Navigating a Graph B D A F C E A C F

  14. A path from v 0 to v n is a list of edges (v 0 , v 1 ), (v 1 , v 2 ), …, (v n-1 , v n ).

  15. The length of a path is the number of edges it contains.

  16. Navigating a Graph B D A F C E

  17. Navigating a Graph B D A F C E

  18. A node v is reachable from node u if there is a path from u to v.

  19. Navigating a Graph B D A F C E

  20. Navigating a Graph B D A F C E

  21. Navigating a Graph B D A F C E B D B

  22. Navigating a Graph B D A F C E B D B D B

  23. Navigating a Graph B D A F C E

  24. Navigating a Graph B D A F C E A B D B D F

  25. A cycle in a graph is a set of edges (v 0 , v 1 ), (v 1 , v 2 ), …, (v n , v 0 ) that starts and ends at the same node.

  26. A simple path is a path that does not contain a cycle. A simple cycle is a cycle that does not contain a smaller cycle

  27. Properties of Nodes B D A F C E

  28. The indegree of a node is the number of edges entering that node. The outdegree of a node is the number of edges leaving that node. In an undirected graph, these are the same and are called the degree of the node.

  29. Summary of Terminology ● A path is a series of edges connecting two nodes. ● The length of a path is the number of edges in the path. ● A node v is reachable from u if there is a path from u to v. ● A cycle is a path from a node to itself. ● A simple path is a path without a cycle. ● A simple cycle is a cycle that does not contain a nested cycle. ● The indegree and outdegree of a node are the number of edges entering/leaving it.

  30. Representing Graphs

  31. Adjacency Matrices B D ● n x n grid of boolean values. A F ● Element A ij is 1 if edge from i to j, 0 else. C E ● Memory usage is O(n 2 ) ● Can check if an edge exists A B C D E F in O(1). A 0 1 1 0 0 0 ● Can find all edges entering B 0 0 0 1 0 0 or leaving a node in O(n). C 0 0 0 0 1 1 D 0 1 0 0 0 1 E 0 0 0 0 0 0 F 0 0 1 0 1 0

  32. Adjacency Lists B D ● List of edges leaving A F each node. ● Memory usage is C E O(m+n) ● Find edges leaving a node in O(d + (u)) A B C B D ● Check if edge exists C E F in O(d + (u)) D B F E F C E

  33. Graph Algorithms

  34. Representing Prerequisites Graph Path Cycle Degree Path Simple Simple Reachability Length Path Cycle

  35. A directed acyclic graph (DAG) is a directed graph with no cycles.

  36. Examples of DAGs

  37. Examples of DAGs 4 2 6 1 3 5 7

  38. Examples of DAGs

  39. Traversing a DAG Graph Path Cycle Degree Path Simple Simple Reachability Length Path Cycle

  40. Traversing a DAG Graph Path Cycle Degree Path Simple Simple Reachability Length Path Cycle

  41. Graph Traversing a DAG Path Cycle Degree Path Simple Simple Reachability Length Path Cycle

  42. Graph Traversing a DAG Path Cycle Degree Path Simple Simple Reachability Length Path Cycle

  43. Graph Traversing a DAG Cycle Path Degree Path Simple Simple Reachability Length Path Cycle

  44. Graph Traversing a DAG Cycle Path Degree Path Simple Simple Reachability Length Path Cycle

  45. Graph Traversing a DAG Cycle Simple Cycle Path Degree Path Simple Reachability Length Path

  46. Graph Traversing a DAG Cycle Simple Cycle Degree Path Path Simple Reachability Length Path

  47. Graph Traversing a DAG Cycle Simple Cycle Degree Path Path Simple Reachability Length Path

  48. Graph Traversing a DAG Cycle Simple Cycle Degree Path Path Simple Reachability Length Path

  49. Graph Traversing a DAG Cycle Simple Cycle Degree Path Path Length Simple Reachability Path

  50. Graph Traversing a DAG Cycle Simple Cycle Degree Path Path Length Reachability Simple Path

  51. Graph Traversing a DAG Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

  52. Graph Traversing a DAG Cycle Graph Simple Cycle Degree Path Cycle Degree Path Path Length Path Simple Simple Reachability Simple Length Path Cycle Path Reachability

  53. Topological Sort ● Order the nodes of a DAG so no node is picked before its parents. ● Algorithm: ● Find a node with no incoming edges (indegree 0) ● Remove it from the graph. ● Add it to the resulting ordering. ● Not necessarily unique. ● Question: When is it unique?

  54. Analyzing Topological Sort ● Assumes at each step that the DAG has a node with indegree zero. Is this always true? ● Claim one: Every DAG has such a node . ● Proof sketch: If this isn't true, then each node has at least one incoming edge. Start at any node and keep following backwards across that edge. Eventually you will find the same node twice and have found a cycle. ● Claim two: Removing such a node leaves the DAG a DAG . ● Proof sketch: If the resulting graph has a cycle, the old graph had a cycle as well.

  55. Traversing an Arbitrary Graph

  56. Traversing an Arbitrary Graph

  57. Traversing an Arbitrary Graph

  58. Traversing an Arbitrary Graph

  59. Traversing an Arbitrary Graph

  60. Traversing an Arbitrary Graph

  61. Traversing an Arbitrary Graph

  62. Traversing an Arbitrary Graph

  63. Traversing an Arbitrary Graph

  64. Traversing an Arbitrary Graph

  65. Traversing an Arbitrary Graph

  66. Traversing an Arbitrary Graph

  67. Traversing an Arbitrary Graph

  68. Traversing an Arbitrary Graph

  69. Traversing an Arbitrary Graph

  70. Traversing an Arbitrary Graph

  71. Traversing an Arbitrary Graph

  72. Traversing an Arbitrary Graph

  73. Traversing an Arbitrary Graph

  74. Traversing an Arbitrary Graph

  75. Traversing an Arbitrary Graph

  76. Traversing an Arbitrary Graph

  77. Traversing an Arbitrary Graph

  78. Traversing an Arbitrary Graph

  79. Traversing an Arbitrary Graph

  80. Traversing an Arbitrary Graph

  81. Traversing an Arbitrary Graph

  82. Traversing an Arbitrary Graph

  83. Traversing an Arbitrary Graph

  84. Traversing an Arbitrary Graph

  85. Traversing an Arbitrary Graph

  86. Traversing an Arbitrary Graph

  87. Traversing an Arbitrary Graph

  88. Traversing an Arbitrary Graph

  89. General Graph Search Algorithm ● Maintain a collection C of nodes to visit. ● Initialize C with some set of nodes. ● While C is not empty: ● Pick a node v out of C. ● Follow all outgoing edges from v, adding each unvisited node found this way to C. ● Eventually explores all nodes reachable from the starting set of nodes. (Why?)

  90. Depth-First Search ● Specialization of the general search algorithm where nodes to visit are put on a stack . ● Explores down a path as far as possible, then backs up. ● Simple graph search algorithm useful for exploring a complete graph. ● Useful as a subroutine in many important graph algorithms. ● Runs in O(m + n) with adjacency lists, O(n 2 ) with adjacency matrix.

  91. Depth-first search A B C D E F

  92. Depth-first search A B C D E F Stack

  93. Depth-first search A B C D E F A Stack

  94. Depth-first search A B C D E F Stack

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend