1 / 110

2620001 Data Structures

2620001 Data Structures. Graph Data Structure. What is a graph?. A data structure that consists of a set of nodes ( vertices ) and a set of edges that relate the nodes to each other The set of edges describes relationships among the vertices. Formal definition of graphs.

tilden
Download Presentation

2620001 Data Structures

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. 2620001Data Structures Graph Data Structure

  2. What is a graph? • A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other • The set of edges describes relationships among the vertices

  3. Formal definition of graphs • A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices)

  4. Directed vs. undirected graphs • When the edges in a graph have no direction, the graph is called undirected

  5. Directed vs. undirected graphs (cont.) • When the edges in a graph have a direction, the graph is called directed (or digraph) Warning: if the graph is directed, the order of the vertices in each edge is important !! E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) (11,1) (9,9)}

  6. Trees vs graphs • Trees are special cases of graphs!!

  7. Graph terminology • Adjacent nodes: two nodes are adjacent if they are connected by an edge • Path: a sequence of vertices that connect two nodes in a graph • Complete graph: a graph in which every vertex is directly connected to every other vertex 5 is adjacent to 7 7 is adjacent from 5

  8. Graph terminology (cont.) • What is the number of edges in a complete directed graph with N vertices?  N * (N-1)

  9. Graph terminology (cont.) • What is the number of edges in a complete undirected graph with N vertices?  N * (N-1) / 2

  10. Graph terminology (cont.) • Weighted graph: a graph in which each edge carries a value

  11. Graph implementation • Array-based implementation • A 1D array is used to represent the vertices • A 2D array (adjacency matrix) is used to represent the edges

  12. Array-based implementation

  13. Graph implementation (cont.) • Linked-list implementation • A 1D array is used to represent the vertices • A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list)

  14. Linked-list implementation

  15. Graph searching • Problem: find a path between two nodes of the graph (e.g., Austin and Washington) • Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)

  16. Depth-First-Search (DFS) • What is the idea behind DFS? • Travel as far as you can down a path • Back up as little as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) • DFS can be implemented efficiently using a stack

  17. Use of a stack • It is very common to use a stack to keep track of: • nodes to be visited next, or • nodes that we have already visited. • Typically, use of a stack leads to a depth-first visit order. • Depth-first visit order is “aggressive” in the sense that it examines complete paths.

  18. Graph Traversals • Both take time: O(V+E)

  19. start end (initialization)

  20. (BFS) • What is the idea behind BFS? • Look at all possible paths at the same depth before you go at a deeper level • Back Breadth-First-Searching up as far as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) • BFS can be implemented efficiently using a Queue

  21. Use of a queue • It is very common to use a queue to keep track of: • nodes to be visited next, or • nodes that we have already visited. • Typically, use of a queue leads to a breadth-first visit order. • Breadth-first visit order is “cautious” in the sense that it examines every path of length i before going on to paths of length i+1.

  22. start end (initialization)

  23. next:

  24. Breadth First Search 2 4 8 s 5 7 3 6 9

  25. Breadth First Search Shortest pathfrom s 1 2 2 4 8 0 s 5 7 3 6 9 Undiscovered Queue: s Discovered Top of queue Finished

  26. Breadth First Search 1 2 4 8 0 s 5 7 3 3 6 9 1 Undiscovered Queue: s 2 Discovered Top of queue Finished

  27. Breadth First Search 1 2 4 8 0 5 s 5 7 1 3 6 9 1 Undiscovered Queue: s 2 3 Discovered Top of queue Finished

  28. Breadth First Search 1 2 4 8 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 Discovered Top of queue Finished

  29. Breadth First Search 1 2 2 4 4 8 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 Discovered Top of queue Finished

  30. Breadth First Search 1 2 2 4 8 5 already discovered:don't enqueue 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished

  31. Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished

  32. Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 3 5 4 Discovered Top of queue Finished

  33. Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 6 9 1 2 Undiscovered Queue: 3 5 4 Discovered Top of queue Finished

  34. Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 3 5 4 6 Discovered Top of queue Finished

  35. Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 5 4 6 Discovered Top of queue Finished

  36. Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 5 4 6 Discovered Top of queue Finished

  37. Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 Discovered Top of queue Finished

  38. Breadth First Search 1 2 3 2 4 8 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 Discovered Top of queue Finished

  39. Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 8 Discovered Top of queue Finished

  40. Breadth First Search 1 2 3 2 4 8 0 7 s 5 7 1 3 3 6 9 1 2 Undiscovered Queue: 6 8 Discovered Top of queue Finished

  41. Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 9 3 1 2 Undiscovered Queue: 6 8 7 Discovered Top of queue Finished

  42. Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 6 8 7 9 Discovered Top of queue Finished

  43. Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 8 7 9 Discovered Top of queue Finished

  44. Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished

  45. Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished

More Related