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.

Slides:



Advertisements
Similar presentations
Review Binary Search Trees Operations on Binary Search Tree
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
CS202 - Fundamental Structures of Computer Science II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Introduction to Graphs
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
Data Structures Using C++ 2E
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COSC 2007 Data Structures II Chapter 14 Graphs I.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Spring 2016CS202 - Fundamental Structures of Computer Science II1 Initially prepared by Dr. Ilyas Cicekli; improved by various Bilkent CS202 instructors.
Graphs Chapter 20.
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Comp 245 Data Structures Graphs.
Graph Algorithm.
Graphs Chapter 13.
Chapter 11 Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Weighted Graphs & Shortest Paths
Chapter 16 1 – Graphs Graph Categories Strong Components
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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. 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 }

Graph Vocabulary Two vertices are adjacent if there is an edge between them. A path exists between two vertices is they are connected by one or more edges. 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.

More Graph Vocabulary 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 A weighted graph has edges labeled with numeric values.

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

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

Adjacency Matrix 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

Adjacency List A set of Lists, one for each vertex. The nodes in the lists represent vertices adjacent to each vertex

A Weighted Directional Graph SFO ABQ LGA PVD ABQ900 LGA2600 LGA1810 SFO2600 PVD150 LGA150

Which To Use? 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.)

A Graph Implementation – A Map of Maps AB200 C450 D1000 BA200 D540 CA450 B300 D100 DB550

Graph Traversals 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.

Depth-First Search Breadth-First Search

Applications: Topological Sorting 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: Spanning Trees 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.

Spanning Trees b f e g c d h i a

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

Minimum Spanning Trees 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.

Prim's Algorithm 1.Starting from a particular vertex, mark that vertex as visited, and add it to the Minimum Spanning Tree. 2.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.

Applications: Minimum Spanning Trees b f e g c d h i a

Applications: Shortest Paths 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.

Dijkstra's Algorithm ∞ 8 ∞ ∞ ∞ 2 ∞ 2 ∞ 3 ∞ 3 ∞ ∞ 2 ∞ 7 4 ∞ ∞ 1 ∞ ∞

Applications: Circuits 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.