© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

CSE 211 Discrete Mathematics
22C:19 Discrete Math Graphs Fall 2010 Sukumar Ghosh.
22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Graph-02.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
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.
Graphs. Overview What is a graph? Some terminology Types of graph Implementing graphs (briefly) Some graph algorithms Graphs 2/18.
Computational problems, algorithms, runtime, hardness
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
Analysis of Algorithms CS 477/677
© 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.
Chapter 11 Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
22C:19 Discrete Math Graphs Spring 2014 Sukumar Ghosh.
GRAPH Learning Outcomes Students should be able to:
Complexity Classes (Ch. 34) The class P: class of problems that can be solved in time that is polynomial in the size of the input, n. if input size is.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Graphs Chapter 12.
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,
Representing and Using Graphs
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
Computer Science: A Structured Programming Approach Using C Graphs A graph is a collection of nodes, called vertices, and a collection of segments,
NP-Complete Problems. Running Time v.s. Input Size Concern with problems whose complexity may be described by exponential functions. Tractable problems.
COSC 2007 Data Structures II Chapter 14 Graphs I.
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.
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++
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.
CS6045: Advanced Algorithms NP Completeness. NP-Completeness Some problems are intractable: as they grow large, we are unable to solve them in reasonable.
Graphs Upon completion you will be able to:
NPC.
CSC 413/513: Intro to Algorithms
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
The NP class. NP-completeness Lecture2. The NP-class The NP class is a class that contains all the problems that can be decided by a Non-Deterministic.
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 Chapter 20.
Csc 2720 Instructor: Zhuojun Duan
Agenda Lecture Content: Introduction to Graph Path and Cycle
EECS 203 Lecture 20 More Graphs.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 11 Limitations of Algorithm Power
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
CSC 380: Design and Analysis of Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs

© 2006 Pearson Addison-Wesley. All rights reserved14 A-2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices, or nodes –A set E of edges A subgraph –Consists of a subset of a graph’s vertices and a subset of its edges Adjacent vertices –Two vertices that are joined by an edge

© 2006 Pearson Addison-Wesley. All rights reserved14 A-3 Terminology Figure 14-2 a) A campus map as a graph; b) a subgraph

© 2006 Pearson Addison-Wesley. All rights reserved14 A-4 Terminology A path between two vertices –A sequence of edges that begins at one vertex and ends at another vertex –May pass through the same vertex more than once A simple path –A path that passes through a vertex only once A cycle –A path that begins and ends at the same vertex A simple cycle –A cycle that does not pass through a vertex more than once

© 2006 Pearson Addison-Wesley. All rights reserved14 A-5 Terminology A connected graph –A graph that has a path between each pair of distinct vertices A disconnected graph –A graph that has at least one pair of vertices without a path between them A complete graph –A graph that has an edge between each pair of distinct vertices

© 2006 Pearson Addison-Wesley. All rights reserved14 A-6 Terminology Figure 14-3 Graphs that are a) connected; b) disconnected; and c) complete

© 2006 Pearson Addison-Wesley. All rights reserved14 A-7 Terminology Multigraph –Not a graph –Allows multiple edges between vertices Figure 14-4 a) A multigraph is not a graph; b) a self edge is not allowed in a graph

© 2006 Pearson Addison-Wesley. All rights reserved14 A-8 Terminology Weighted graph –A graph whose edges have numeric labels Figure 14-5a a) A weighted graph

© 2006 Pearson Addison-Wesley. All rights reserved14 A-9 Terminology Undirected graph –Edges do not indicate a direction Directed graph, or diagraph –Each edge is a directed edge Figure 14-5b b) A directed graph

© 2006 Pearson Addison-Wesley. All rights reserved14 A-10 Terminology Directed graph –Can have two edges between a pair of vertices, one in each direction –Directed path A sequence of directed edges between two vertices –Vertex y is adjacent to vertex x if There is a directed edge from x to y

© 2006 Pearson Addison-Wesley. All rights reserved14 A-11 A Quick Tangent The Traveling Salesperson... and every other hard problem in computing...

© 2006 Pearson Addison-Wesley. All rights reserved14 A-12 The Traveling Salesperson Problem A Hamilton circuit is a cycle that visits every vertex in a graph exactly once The T.S.P. asks us to find a minimal cost Hamilton circuit in a weighted, directed graph – idea: a salesperson trying to find the cheapest route between all cities on a map

© 2006 Pearson Addison-Wesley. All rights reserved14 A-13 Example

© 2006 Pearson Addison-Wesley. All rights reserved14 A-14 How to solve it? The easiest solution is to simply try all possibilities. Problem: –Suppose there are N cities, all connected –Then there are N! possible paths –For 10 cities: about 3.5 million combinations –For 100 cities: ×

© 2006 Pearson Addison-Wesley. All rights reserved14 A-15 How to solve it? Branch and bound algorithms: –Divide and conquer principle –Partition cities into sub-regions (branch) –Find bounds on all the solutions contained in each sub-region Exact solution is possible... for around cities

© 2006 Pearson Addison-Wesley. All rights reserved14 A-16 Can we solve it quickly? What do we mean by quickly? We know that about O(n), O(n 2 ),... A problem is solvable in polynomial time if it is in O(n k ) for some k In theoretical computing science, polynomial time problems are considered “tractable”

© 2006 Pearson Addison-Wesley. All rights reserved14 A-17 Can we solve it quickly? Quick answer: no More accurate answer: probably not Actual answer: –There is no known polynomial solution to TSP –But it is more interesting...

© 2006 Pearson Addison-Wesley. All rights reserved14 A-18 Finding Versus Checking There is a difference between finding solutions and checking solutions Consider: –Find all students at SFU with green eyes –Take a random set of SFU students, check if they all have green eyes

© 2006 Pearson Addison-Wesley. All rights reserved14 A-19 P versus NP The set of problems solvable in polynomial time is called P The set of problems that are “verifiable” in polynomial time is called NP (this is skipping over A LOT of details)

© 2006 Pearson Addison-Wesley. All rights reserved14 A-20 Is P=NP? This is unknown, but it is widely considered to be the most important problem in theoretical computer science Why so important? –Among other things... if P=NP... then virtually all modern cryptosystems are “easily” breakable

© 2006 Pearson Addison-Wesley. All rights reserved14 A-21 What about TSP? TSP is in NP... you can efficiently check if a solution is optimal What’s more: EVERY problem in NP is equivalent to TSP So: A polynomial time solution to TSP would prove that P=NP

© 2006 Pearson Addison-Wesley. All rights reserved14 A-22 What does this mean for you? Fame and fortune? –Yes... see Andrew Wiles cover photo on Time magazine for Fermat’s last theorem –Also there is a $1 million prize for a solution At this point, people take it for granted that the answer is “No”... but a proof is still a big, big deal to many people

© 2006 Pearson Addison-Wesley. All rights reserved14 A-23 Graphs As ADTs Graphs can be used as abstract data types Two options for defining graphs –Vertices contain values –Vertices do not contain values Operations of the ADT graph –Create an empty graph –Determine whether a graph is empty –Determine the number of vertices in a graph –Determine the number of edges in a graph

© 2006 Pearson Addison-Wesley. All rights reserved14 A-24 Graphs As ADTs Operations of the ADT graph (Continued) –Determine whether an edge exists between two given vertices; for weighted graphs, return weight value –Insert a vertex in a graph whose vertices have distinct search keys that differ from the new vertex’s search key –Insert an edge between two given vertices in a graph –Delete a particular vertex from a graph and any edges between the vertex and other vertices –Delete the edge between two given vertices in a graph –Retrieve from a graph the vertex that contains a given search key

© 2006 Pearson Addison-Wesley. All rights reserved14 A-25 Implementing Graphs Most common implementations of a graph –Adjacency matrix –Adjacency list Adjacency matrix –Adjacency matrix for a graph with n vertices numbered 0, 1, …, n – 1 An n by n array matrix such that matrix[i][j] is –1 (or true) if there is an edge from vertex i to vertex j –0 (or false) if there is no edge from vertex i to vertex j

© 2006 Pearson Addison-Wesley. All rights reserved14 A-26 Implementing Graphs Figure 14-6 a) A directed graph and b) its adjacency matrix

© 2006 Pearson Addison-Wesley. All rights reserved14 A-27 Implementing Graphs Adjacency matrix for a weighted graph with n vertices numbered 0, 1, …, n – 1 –An n by n array matrix such that matrix[i][j] is The weight that labels the edge from vertex i to vertex j if there is an edge from i to j  if there is no edge from vertex i to vertex j Figure 14-7 a) A weighted undirected graph and b) its adjacency matrix

© 2006 Pearson Addison-Wesley. All rights reserved14 A-28 Implementing Graphs Adjacency list –An adjacency list for a graph with n vertices numbered 0, 1, …, n – 1 Consists of n linked lists The i th linked list has a node for vertex j if and only if the graph contains an edge from vertex i to vertex j –This node can contain either »Vertex j’s value, if any »An indication of vertex j’s identity

© 2006 Pearson Addison-Wesley. All rights reserved14 A-29 Implementing Graphs Figure 14-8 a) A directed graph and b) its adjacency list

© 2006 Pearson Addison-Wesley. All rights reserved14 A-30 Implementing Graphs Adjacency list for an undirected graph –Treats each edge as if it were two directed edges in opposite directions Figure 14-9 a) A weighted undirected graph and b) its adjacency list

© 2006 Pearson Addison-Wesley. All rights reserved14 A-31 Implementing Graphs Adjacency matrix compared with adjacency list –Two common operations on graphs Determine whether there is an edge from vertex i to vertex j Find all vertices adjacent to a given vertex i –Adjacency matrix Supports operation 1 more efficiently –Adjacency list Supports operation 2 more efficiently Often requires less space than an adjacency matrix

© 2006 Pearson Addison-Wesley. All rights reserved5 B-32 Implementing a Graph Class Using the JCF ADT graph not part of JCF Can implement a graph using an adjacency list consisting of a vector of maps Implementation presented uses TreeSet class

© 2006 Pearson Addison-Wesley. All rights reserved14 A-33 Graph Traversals A graph-traversal algorithm –Visits all the vertices that it can reach –Visits all vertices of the graph if and only if the graph is connected A connected component –The subset of vertices visited during a traversal that begins at a given vertex –Can loop indefinitely if a graph contains a loop To prevent this, the algorithm must –Mark each vertex during a visit, and –Never visit a vertex more than once

© 2006 Pearson Addison-Wesley. All rights reserved14 A-34 Graph Traversals Figure Visitation order for a) a depth-first search; b) a breadth-first search

© 2006 Pearson Addison-Wesley. All rights reserved14 A-35 Exercise Give the adjacency matrix & list for this graph: a b d e f c

© 2006 Pearson Addison-Wesley. All rights reserved14 A-36 Adjacency Matrix ABCDEF A B C D E F001100

© 2006 Pearson Addison-Wesley. All rights reserved14 A-37 Adjacency List BCACABDEFCFCCD A B C D E F

© 2006 Pearson Addison-Wesley. All rights reserved14 A-38 Exercise Consider the graph in figure 14-6 (and 14- 8). Show that the adjacency matrix for this graph requires more memory than the adjacency list.

© 2006 Pearson Addison-Wesley. All rights reserved14 A-39 Solution Suppose each integer require m bytes and assume that addresses are stored as integers. The size of the adjacency matrix is then: (9 * 9)m = 81m bytes. The adjacency list requires an array of 9 addresses of size m plus ten nodes each composed of an integer for storing the vertex name and another integer for the address of the next node (if any): 9m + (10 * 2)m = 29m bytes.