Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs and Digraphs Chapter 14.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Chapter 9: Graphs Basic Concepts
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
Computer Science 112 Fundamentals of Programming II Introduction to 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.
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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
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.
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.
1/16/20161 Introduction to Graphs Advanced Programming Concepts/Data Structures Ananda Gunawardena.
1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
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.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
CS212: Data Structures and Algorithms
Introduction to Graphs
CS 367 – Introduction to Data Structures
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Chapter 9: Graphs Basic Concepts
Graphs.
Graph Operations And Representation
Chapter 11 Graphs.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Chapter 9: Graphs Basic Concepts
GRAPHS.
Introduction to Graphs
For Friday Read chapter 9, sections 2-3 No homework
Introduction to Graphs
Presentation transcript:

Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs

Objects and Connections Many problems are naturally formulated in terms of objects and the connections between them. For example: Airline Routes--What is the fastest (or cheapest) way to get from one city to another? Electrical circuits--Circuit elements are wired together. How does the current flow? Job Scheduling--The objects are tasks that need to be performed. The connections indicate which jobs should be done before which other jobs. Links between web pages A graph is a mathematical object that describes such situations. Algorithms for graphs are fundamental to CS. Graph Theory is a major branch of combinatorial mathematics.

Graphs A graph is a collection of vertices, V, and edges, E. An edge connects two vertices. a d c b a d c b is the same as: Vertices: a, b, c, d Edges: ab, bc, ac, ad

Definitions A path from one vertex to another is a list of vertices in which successive vertices are connected by edges in the graph. a d c b Example: A path from a to c could be ac or abc. A graph is connected if there is a path from every node to every other node in the graph. The above graph is connected. a d c b e Not Connected

More definitions A simple path is a path with no vertex repeated. A simple cycle is a simple path except the first and last vertex is repeated and, for an undirected graph, number of vertices >= 3. Example: abca is a cycle a d c b A tree is a graph with no cycles. a d c b

Definitions Continued A complete graph is a graph in which all edges are present. A sparse graph is a graph with relatively few edges. A dense graph is a graph with many edges. a d c b Complete a d c b Dense a d c b Sparse

Types of Graphs An undirected graph has no specific direction between the vertices. A directed graph has edges that are "one way". We can go from one vertex to another, but not vice versa. A weighted graph has weights associated with each edge. The weights can represent distances, costs, etc. a d c b 34 22 13 19 4 Weighted a d c b Undirected a d c b Directed

Representing Graphs as an Adjacency List An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge. List 1 1 2 3 2 5 4 3 4 5 Definition: A digraph G consists of a set V, called the vertices of G, and for all v in V, a subset Av of V, called the set of vertices adjacent to v.

Representing Graphs as an Adjacency List An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge. List 1 2 5 1 2 1 5 4 3 3 2 2 4 3 5 4 2 5 3 4 1 2 4 5 Definition: A digraph G consists of a set V, called the vertices of G, and for all v in V, a subset Av of V, called the set of vertices adjacent to v.

Representing a Graph with an Adjacency Matrix An adjacency matrix is a matrix with a row and column for each vertex. The matrix entry is 1 if there is an edge between the row vertex and the column vertex. 1 2 1 2 3 4 5 1 0 1 0 0 1 2 1 0 1 1 1 3 0 1 0 1 0 4 0 1 1 0 1 5 1 1 0 1 0 3 5 4 The diagonal may be zero or one. Each edge is represented twice.

Representing Directed Graphs In directed graphs we only include in the list those vertices that are pointed to by a given vertex. List 1 2 3 1 2 4 5 6 3 1 2 3 4 5 6 1 2 3 4 5 6 4 5 6

Representing Directed Graphs In directed graphs we only include in the list those vertices that are pointed to by a given vertex. List 1 2 3 1 4 2 5 2 4 5 6 5 6 3 1 2 3 4 5 6 1 0 1 0 1 0 0 2 0 0 0 0 1 0 3 0 0 0 0 1 1 4 0 1 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 0 1 2 4 4 5 6 6

Representing Weighted Graphs In weighted graphs we include the weights of each edge. 2 List 1 2 3 1 1 3 6 5 7 2 4 4 5 6 1 3 1 2 3 4 5 6 1 2 3 4 5 6 4 5 6

Representing Weighted Graphs In weighted graphs we include the weights of each edge. 2 List 1 2 3 1 4 1 2 2 1 3 6 5 7 5 5 2 4 4 5 6 1 5 6 6 7 3 1 2 3 4 5 6 1 0 2 0 1 0 0 2 0 0 0 0 5 0 3 0 0 0 0 6 7 4 0 3 0 0 0 0 5 0 0 0 4 0 0 6 0 0 0 0 0 1 2 3 4 4 4 5 6 6 1

Lists vs. Matrices Speed: An adjacency matrix provides the fastest way to determine whether or not a particular edge is present in the graph. For an adjacency list, there is no faster way than to search the entire list for the presence of the edge. Memory: Normally, an adjacency matrix requires more space (n2 entries). However, if n is small and the graph is unweighted, an adjacency matrix only needs 1 bit per entry. The adjacency list requires at least 1 word per entry (for the address of the next node of the list). This may offset the advantage of fewer entries.

Breadth First Traversal Problem: Given a graph, G = (V, E), find all the vertices reachable from some source, s. Repeat for all unvisited vertices Strategy: Visit all vertices adjacent to s first. Traversal expands outward level by level. (Visit all vertices a distance of 2 away from s second, then those at distance 3, etc.) Keep track of nodes that have been visited already (Otherwise may visit a vertex twice or end up in an endless cycle).

Pseudocode for Breadth First Traversal template <int max_size> void Digraph <max_size>:: breadth_first(void (*visit)(vertex &)) const { Queue q; bool visited[max_size]; Vertex v, w, x; for (all v in G) visited[v] = false; for (all v in G) if (!visited[v]) { q.append(v); while(!q.empty( )) { q.retrieve(w); if(!visited[w]) { visited[w] = true; (*visit)(w); for (all x adjacent to w) q.append(x); } q.serve( );

Example a b c d e f g h We will work through this in class.