1 / 25

Graphs

Graphs. Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are a subset of the graphs we’re going to talk about.

kitty
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

  2. Similar to the graphs you’ve known since the 5th grade: line graphs, bar graphs, etc., but more general. • Those mathematical graphs are a subset of the graphs we’re going to talk about. • A graph is made up of a set of vertices (or nodes) and a set of edges (or lines) that connect the vertices. G = { V, E } Graphs

  3. Two vertices are adjacentif there is an edge between them. • A path exists between two vertices is they are connected by one or moreedges. • A simple path may not pass through the same vertex more than one time. • A path that begins and ends at the same vertex is known as a cycle. • A graph is connected if there is a path between every pair of vertices, disconnected,if not. Graph Vocabulary

  4. In a complete graph, each pair of vertices has an edge between them. • In a directed graph, or digraph, a direction is indicated, and movement can only be in the direction indicated. • There may be two edges between a pair of vertices, one in each direction. More Graph Vocabulary

  5. A weighted graph has edges labeled with numeric values. More Graph Vocabulary

  6. Create an empty graph. • Determine whether a graph is empty. • Determine the number of vertices and/or edges in a graph. • Does an edge exist between 2 vertices? • Insert a vertex that has a unique key. • Insert an edge between 2 vertices. • Delete an edge between 2 vertices. • Delete a vertex and all edges to it. • Retrieve the vertex that has a given key. Operations of a Graph ADT

  7. As an adjacency matrix or two-dimension array. • As an adjacency list or an array of linked lists. Two Ways to Represent a Graph

  8. One row and one column for each vertex • Cell contains a 1 if edge exists between i and j, 0 if edge doesn’t exist. • If weighted, matrix can contain weight instead of 1. 0 1 2 3 0 0 1 1 0 1 0 0 1 0 2 1 0 0 1 3 0 0 1 0 Adjacency Matrix

  9. A set of Lists, one for each vertex. • The nodes in the lists represent vertices adjacent to each vertex. 1 2 2 0 3 2 0 1 2 Adjacency List 3

  10. SFO PVD ABQ SFO LGA LGA LGA 1810 150 150 2600 2600 900 ABQ A Weighted Directional Graph LGA PVD

  11. Decision should be based on the application. • In most cases, list will use less memory than matrix. • Some operations are more efficient with a matrix( Is there an edge from i to j?) • Others are more efficient with a list (Find all vertices adjacent to i.) Which To Use?

  12. A Graph Implementation – A Map of Maps

  13. Traversal visits every vertex that is connected. • You can use a traversal to find out if, in fact, the graph is connected. If you traverse and visit every vertex, the graph is connected. • The traversal algorithm must keep track of vertices visited, so it doesn’t visit a vertex more than once, or an infinite loop may result. • Two types of traversals: Depth-First and Breadth-First. Graph Traversals

  14. Breadth-First Search Depth-First Search

  15. Topological Order – A directed graph without cycles has a natural order. • There may be several topological orders in a graph. • Topological Sorting – Arranging the vertices into a Topological Order. Applications: Topological Sorting

  16. A Spanning Tree is a sub-graph of graph G that has all of its vertices and enough of its edges to form a tree. • G must be an undirected graph with no cycles. • A connected, undirected graph with n vertices must have at least n-1 edges. • A connected, undirected graph with n vertices and n-1 edges cannot contain a cycle. • A connected, undirected graph with n vertices and more than n-1 edges must contain a cycle. Applications: Spanning Trees

  17. b a c h e d i g f Spanning Trees

  18. a b b a c i f e c h e d i g d g h f

  19. Find the minimum cost path to visit all vertices in a weighted undirected graph. • Minimum Spanning Trees may not be unique, but all will have the same cost. • One simple way to find an MST is to use Prim’s Algorithm. Minimum Spanning Trees

  20. Starting from a particular vertex, mark that vertex as visited, and add it to the Minimum Spanning Tree. While there are unvisited vertices: Find the minimum value edge from any one of the visited vertices to one of the unvisited vertices. Add that edge to the Minimum Spanning Tree, and mark the terminating vertex as visited. Prim's Algorithm

  21. 6 b 7 a 9 c 2 h 4 3 e 1 d 4 i 8 5 g 2 f Applications: Minimum Spanning Trees

  22. In a weighted, directed graph, find the "shortest" path between two vertices. • The weight or cost of a path is the sum of the weights of the edges in the path. • Dijkstra's shortest-path algorithm actually finds the shortest paths between a specified vertex and all other vertices. Applications: Shortest Paths

  23. 8 2 0 1 2 1 4 3 2 1 9 3 4 7 0 1 2 3 4 0 ∞ 8 ∞ 9 4 1 ∞ ∞ 1 ∞ ∞ 2 ∞ 2 ∞ 3 ∞ 3 ∞ ∞ 2 ∞ 7 4 ∞ ∞ 1 ∞ ∞ Dijkstra's Algorithm

  24. A circuit is a type of cycle that visits each vertex or each edge exactly once. • An Euler circuit is one that begins at a specified vertex, passes through each edge exactly once, and ends at the same vertex. • Only exists if every vertex has an even number of edges. Applications: Circuits

More Related