1 / 46

Graphs

Graphs. Chapter 13. Preview Graphs are an important mathematical concept that have significant applications not only in computer science, but also in many other fields. You can view a graph as a mathematical construct, a data structure, or an abstract data type.

Patman
Download Presentation

Graphs

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. Graphs Chapter 13

  2. Preview • Graphs are an important mathematical concept that have significant applications not only in computer science, but also in many other fields. • You can view a graph as a mathematical construct, a data structure, or an abstract data type. • This chapter provides an introduction to graphs and it presents the major operations and applications of graphs that are relevant to the computer scientist Chapter 13 -- Graphs

  3. Terminology • Def: A graph G consists of two sets • Vertices • Edges • Def: Adjacent • Two vertices are said to be adjacent if they are joined by an edge. Chapter 13 -- Graphs

  4. Def: Subgraph • A subgraph consists of a set of a graph’s vertices and a subset of its edges Chapter 13 -- Graphs

  5. Def: Path • A path between two vertices is a sequence of edges that begins at one vertex and ends at another. • Simple path: does not pass through the same vertex more than once. • Def: Cycle • A cycle is a path that begins and ends at the same vertex. • Def: Connected • A graph is connected if each pair of distinct vertices has a path between them Chapter 13 -- Graphs

  6. A graph is complete if each pair of distinct vertices has an edge between them Chapter 13 -- Graphs

  7. Since a graph has a set of edges it cannot have duplicate edges. • However, a multigraph does allow multiple edges • A graph also does not allow self-edges (loops) Chapter 13 -- Graphs

  8. You can label the edges of a graph • When these labels represent numeric values, the graph is called a weighted graph Chapter 13 -- Graphs

  9. All of the previous examples were of undirected graphs, because the edges did not indicate a direction. If there is direction, then the graph is called a directed graph (or digraph) Chapter 13 -- Graphs

  10. Graphs as ADTs • Basic Functions • Create, Destroy, Determine empty • Determine number of vertices and edges • Is there an edge between I and J? • Insert a vertex, or Insert an edge • Delete a vertex, or Delete an Edge • … Chapter 13 -- Graphs

  11. Implementing Graphs • The two most common implementations of a graph are the Adjacency Matrix and the Adjacency List • Adjacency Matrix • Given a graph G with n vertices • Matix is n x n of Booleans • Entries – True if an edge exists between I and J (false otherwise). Chapter 13 -- Graphs

  12. Example: a directed graph. Chapter 13 -- Graphs

  13. Example: a weighted graph Chapter 13 -- Graphs

  14. An Adjacency List • Given a graph G with n vertices • N linked lists • The ith list has node for vertex j iff there is an edge between i and j Chapter 13 -- Graphs

  15. Example: a directed graph Chapter 13 -- Graphs

  16. Example: a weighted graph Chapter 13 -- Graphs

  17. Which implementation is better? • Let’s look at the two most common operations • Determine if there is an edge (i, j) • Find all vertices adjacent to a given vertex i. • The adjacency matrix supports the first one somewhat more efficiently than the list • The second operation is supported more efficiently by the adjacency list than the matrix. Chapter 13 -- Graphs

  18. Now consider the space requirements Chapter 13 -- Graphs

  19. Graph Traversals • Visit all the vertices in the graph. • There are two major ways to do this: • Depth first search • Breadth first search Chapter 13 -- Graphs

  20. Example: the order in which the nodes of a tree are visited in a depth-first search and a breadth-first search. Chapter 13 -- Graphs

  21. How do you implement them? • Depth-First Search • Typically done with a stack. • Breadth-First Search • Typically done with a Queue Chapter 13 -- Graphs

  22. Example: DFS • Start with Node a • Trace the Search Chapter 13 -- Graphs

  23. Example: DFS Chapter 13 -- Graphs

  24. Example: BFS • Start with Node a • Trace the Search Chapter 13 -- Graphs

  25. Example: BFS Chapter 13 -- Graphs

  26. Applications of Graphs • Topological Sorting • Given the following graph, place the vertices in order so that the edges all point in one direction Chapter 13 -- Graphs

  27. Multiple solutions: • Applications: Prerequisites ( 201,202,236,336,308,…) Chapter 13 -- Graphs

  28. Spanning Trees • Given a graph G, construct a tree that has all the vertices of G Chapter 13 -- Graphs

  29. A few observations about undirected graphs • A connected undirected graph that has n vertices must have at least n-1 edges • A connected undirected graph that has n vertices and exactly n-1 edges cannot contain a cycle. • A connected undirected graph that has n vertices and more than n-1 edges must contain at least 1 cycle. Chapter 13 -- Graphs

  30. The DFS Spanning Tree Chapter 13 -- Graphs

  31. The BFS spanning Tree Chapter 13 -- Graphs

  32. Minimum Spanning Trees • Given a weighted undirected graph, compute the spanning tree with the minimum cost Chapter 13 -- Graphs

  33. Prim’s Algorithm • Start with two sets • Vertices not visited (not part of the tree) • Vertices visited (part of the tree – initialized to some start vertex) • Find the shortest edge from the visited set to the not visited set. • Add the edge to the MST • Move the vertex on the other end from not visited to visited. • Continue until all vertices are visited. Chapter 13 -- Graphs

  34. Trace of Prim’s Chapter 13 -- Graphs

  35. Chapter 13 -- Graphs

  36. Chapter 13 -- Graphs

  37. Shortest Paths • Consider once again a map of airline routes. • The shortest path from 0 to 1 in the following graph is not the edge 0-1 (weight 8) but the path (0-4-2-1) Chapter 13 -- Graphs

  38. The algorithm to calculate the answer is attributed to Dijkstra • Need two arrays • Weight – Weight[v] = weight of the shortest path from 0 to v • Matrix – Matrix[v][u] = weight of edge from v to u • Then compare the Weight[u] with Weight[v]+Matrix[v][u]. Put the smallest in Weight[u] • Then we need a set of vertices we have visited Chapter 13 -- Graphs

  39. We begin with the vertex set initialized to 0 Chapter 13 -- Graphs

  40. Chapter 13 -- Graphs

  41. Chapter 13 -- Graphs

  42. Circuits • Eulerian Circuits • Probably the first application of graphs occurred in the early 1700s when Euler proposed the Königsberg bridge problem. • Can you start and end at the same point and only cross each bridge once? Chapter 13 -- Graphs

  43. Some Difficult Problems • The Traveling Salesperson Problem • Def: A Hamiltonian circuit – pass through every vertex once. • Given • a weighted graph • an origin city • Problem: • the salesperson must begin at an origin city and visit all others (exactly once) and return to the origin • and do it with the least cost. Chapter 13 -- Graphs

  44. The three utilities problem • Given • A graph (3 houses, 3 utilities) • Problem • Connect each house with each utility so the edges do not cross • This is a planarity question (minimum crossing number = 0) for K3,3 Chapter 13 -- Graphs

  45. The four color problem • Given • A planar graph • Problem • Can you color the vertices so no two adjacent vertices have the same color? • The question was posed more than a century ago (1852) and was only solved in the 1976 (with the help of a computer) • Trivia: Nature July 17, 1879 carried a proof of this problem (which was proved false in 1890) Chapter 13 -- Graphs

  46. Summary • Implementations: • Adjacency Matrix and Adjacency List • Graph searching • uses stacks and Queues • Trees • Connected, undirected, acyclic graphs • Problems looked at • Shortest Paths, Eulerian circuits, Hamiltonian Circuits Chapter 13 -- Graphs

More Related