graphs
play

Graphs Tessema M. Mengistu Department of Computer Science Southern - PowerPoint PPT Presentation

Graphs Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - Faner 3131 1 Outline Introduction to Graphs Graph Traversals Finding a Path Graph ADT 2


  1. Graphs Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - Faner 3131 1

  2. Outline • Introduction to Graphs • Graph Traversals • Finding a Path • Graph ADT 2

  3. Introduction to Graph • There are many applications that uses graphs – Google Map 3

  4. Introduction to Graph • Airline Reservation Systems 4

  5. Introduction to Graph • Global Positioning System (GPS) 5

  6. Introduction to Graph • Graph – Consists of a finite set (distinct) of vertices or nodes or points that are connected together using lines called edges – A subgraph is a portion of a graph that is itself a graph 6

  7. Introduction to Graph 7

  8. Graph Terminology • Nodes connected by edges • Edges – Undirected – Directed (digraph)

  9. Graph Terminology • Path between two vertices – Sequence of edges • A path in a directed graph must consider the direction of the edges, and is called a directed path. • The length of a path is the number of edges that it comprises. • If the path does not pass through any vertex more than once, it is a simple path. • Graph can be: – Cyclic – has a path that begins and ends at the same vertex – Acyclic - A graph that has no cycles is 9

  10. Graph Terminology • Weights – Shortest, fastest, cheapest, costs • A weighted graph – has values on its edges – E.g distance in miles, cost, time of driving, etc • A path in a weighted graph has a weight, or cost, which is the sum of its edge weights 10

  11. Graph Terminology • Connected graphs – A graph that has a path between every pair of distinct vertices • A complete graph – Has an edge between every pair of distinct vertices. 11

  12. Graph Terminology • Adjacent vertices – Undirected graph • Two vertices are adjacent if they are joined by an edge • Also are called neighbors – Directed graph • Vertex i is adjacent to vertex j if a directed edge begins at j and ends at i. • Vertex A is adjacent to vertex B, but vertex B is not adjacent to vertex A. 12

  13. Graph Terminology • If a graph has n vertices, it can have at most – n (n - 1) edges if the graph is directed – n (n - 1) / 2 edges if the graph is undirected • A graph is sparse if it has relatively few edges – Has O( n) edges • A graph is dense if it has many edges – Has O( n2) edges. • Typical graphs are sparse. 13

  14. Graphs Vs Trees • All trees are graphs, but not all graphs are trees. • A tree is a connected graph without cycles. 14

  15. Graph Traversals • Graph Traversals – Focus on the connections between vertices, rather than the contents of vertices • In a graph, “visit a node” means simply to “mark the node as visited.” • Begins at any vertex — called the origin vertex — and visits only the vertices that it can reach . • Two types – Breadth-first – Depth-first 15

  16. Breadth-first Traversal • Breadth-first traversal – Visits all neighbors of a node before visiting the neighbors’ neighbors. • Traversal uses a queue to hold the visited vertices. • The traversal order is then the order in which vertices are added to the queue. 16

  17. Breadth-first Traversal 17

  18. 18

  19. Breadth-first Traversal • Example 19

  20. Depth-first Traversal • Depth-first traversal – Follows a path that goes as deeply into the graph as possible before following other paths. – After visiting a vertex, this traversal visits the vertex’s neighbor, the neighbor’s neighbor, and so on. – Uses a stack in the iterative description of this traversal. 20

  21. Depth-first Traversal 21

  22. 22

  23. Depth-first Traversal • Example 23

  24. Path • Path examples – Learning whether a particular airline flies between two given cities – Finding if there is a road connecting two cities • A depth-first traversal stays on a path through the graph as it visits as many vertices as possible. – Each time we visit another vertex, we see whether that vertex is the desired destination. – If so, we are done and the resulting stack contains the path. – Otherwise, we continue the traversal until either we are successful or the traversal ends. 24

  25. Shortest Path – unweighted Graph • A graph can have several different paths between the same two vertices. • We can find the path with the shortest length, that is, the path that has the fewest edges. – Example – a path between A and H 25

  26. Algorithm • Every node keeps – Its predecessor node – A length of the path to reach that specific node – Example 26

  27. 27

  28. 28

  29. Shortest Path – Weighted Graph • The shortest path is not necessarily the one with the fewest edges. • The Shortest path is the one with the smallest edge-weight sum. • Example 29

  30. Figure 28-19 A trace of the traversal in the algorithm to find the cheapest path from vertex A to vertex H in a weighted graph

  31. Figure 28-19 A trace of the traversal in the algorithm to find the cheapest path from vertex A to vertex H in a weighted graph

  32. FIGURE 28-20 The graph in Figure 28-18a after finding the cheapest path from vertex A to vertex H

  33. 33

  34. 34

  35. Graph ADT • No addition, removal, or retrieval components. • We use a graph to answer questions based on the relationships among its vertices. 35

  36. • Example 36

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