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.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

Part A - Terminology and Traversals
Graphs and Digraphs Chapter 14.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Algorithms and Data Structures
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
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)
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.
CS202 - Fundamental Structures of Computer Science II
Graphs CS3240, L. grewe.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
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.
James Tam Introduction To Graphs In this section of notes you will learn about a new ADT: graphs.
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.
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
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.
Data Structures Using C++ 2E
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 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,
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
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.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
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 Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
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++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
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.
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 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.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
Graphs Chapter 20.
Graphs.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 11 Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

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 two vertices Edges can be undirected or directed (digraph) A subgraph is a portion of a graph that itself is a graph

3 A portion of a road map treated as a graph Nodes Edges

4 A directed graph representing part of a city street map

5 (a) A maze; (b) its representation as a graph

6 The prerequisite structure for a selection of courses as a directed graph without cycles.

7 Paths A sequence of edges that connect two vertices in a graph In a directed graph the direction of the edges must be considered Called a directed path A cycle is a path that begins and ends at same vertex and does not pass through any vertex more than once A graph with no cycles is acyclic

8 Weights A weighted graph has values on its edges Weights, costs, etc. A path in a weighted graph also has weight or cost The sum of the edge weights Examples of weights Miles between nodes on a map Driving time between nodes Taxi cost between node locations

9 A weighted graph

10 Connected Graphs A connected graph Has a path between every pair of distinct vertices A complete graph Has an edge between every pair of distinct vertices A disconnected graph Not connected

11 Connected Graphs Undirected graphs

12 Adjacent Vertices Two vertices are adjacent in an undirected graph if they are joined by an edge Sometimes adjacent vertices are called neighbors Vertex A is adjacent to B, but B is not adjacent to A.

13 Note the graph with two subgraphs Each subgraph connected Entire graph disconnected Airline routes

14 Trees All trees are graphs But not all graphs are trees A tree is a connected graph without cycles Traversals Preorder (and, technically, inorder and postorder) traversals are examples of depth- first traversal Level-order traversal of a tree is an example of breadth-first traversal Visit a node Process the node’s data and/or mark the node as visited

15 Trees Visitation order of two traversals; (a) depth first; (b) breadth first.

16 Depth-First Traversal Visits a vertex, then A neighbor of the vertex, A neighbor of the neighbor, Etc. Advance as far as possible from the original vertex Then back up by one vertex Considers the next neighbor

17 Algorithm depthFirstTraversal (originVertex) traversalOrder = a new queue for the resulting traversal order vertexStack = a new stack to hold vertices as they are visited Mark originVertex as visited traversalOrder.enqueue (originVertex) vertexStack.push (originVertex) while (!vertexStack.isEmpty ()) { topVertex = vertexStack.peek () if (topVertex has an unvisited neighbor) { nextNeighbor = next unvisited neighbor of topVertex Mark nextNeighbor as visited traversalOrder.enqueue (nextNeighbor) vertexStack.push (nextNeighbor) } else // all neighbors are visited vertexStack.pop () } return traversalOrder

18 Depth-First Traversal Trace of a depth-first traversal beginning at vertex A. Assumes that children are placed on the stack in alphabetic (or numeric order).

19 Breadth-First Traversal Algorithm for breadth-first traversal of nonempty graph beginning at a given vertex Algorithm breadthFirstTraversal(originVertex) vertexQueue = a new queue to hold neighbors traversalOrder = a new queue for the resulting traversal order Mark originVertex as visited traversalOrder.enqueue(originVertex) vertexQueue.enqueue(originVertex) while (!vertexQueue.isEmpty()) { frontVertex = vertexQueue.dequeue() while (frontVertex has an unvisited neighbor) { nextNeighbor = next unvisited neighbor of frontVertex Mark nextNeighbor as visited traversalOrder.enqueue(nextNeighbor) vertexQueue.enqueue(nextNeighbor) } } return traversalOrder A breadth-first traversal visits a vertex and then each of the vertex's neighbors before advancing

20 Breadth-First Traversal A trace of a breadth-first traversal for a directed graph, beginning at vertex A. Assumes that children are placed in the queue in alphabetic (or numeric order).

21 Implementations of the ADT Graph A directed graph and implementations using adjacency lists and an adjacency matrix. ABCD A0111 B0000 C0000 D1010

22

23 Topological Order Given a directed graph without cycles In a topological order Vertex a precedes vertex b whenever a directed edge exists from a to b

24 Topological Order Three topological orders for the indicated graph.

25 Topological Order An impossible prerequisite structure for three courses as a directed graph with a cycle.

26 Topological Order Algorithm for a topological sort Algorithm getTopologicalSort() vertexStack = a new stack to hold vertices as they are visited n = number of vertices in the graph for (counter = 1 to n) { nextVertex = an unvisited vertex having no unvisited successors Mark nextVertex as visited stack.push(nextVertex) } return stack

27 Topological Sorting Algorithm getTopologicalSort() vertexStack = a new stack to hold vertices as they are visited n = number of vertices in the graph for (counter = 1 to n) { nextVertex = an unvisited vertex having no unvisited successors Mark nextVertex as visited stack.push(nextVertex) } return stack

28 Topological Order Finding a topological order

29 Shortest Path in an Unweighted Graph (a) an unweighted graph and (b) the possible paths from vertex A to vertex H.

30 Shortest Path in an Unweighted Graph The previous graph after the shortest-path algorithm has traversed from vertex A to vertex H

31 Shortest Path in an Unweighted Graph Finding the shortest path from vertex A to vertex H.

32 Shortest Path in an Weighted Graph (a) A weighted graph and (b) the possible paths from vertex A to vertex H.

33 Shortest Path in an Weighted Graph Shortest path between two given vertices Smallest edge-weight sum Algorithm based on breadth-first traversal Several paths in a weighted graph might have same minimum edge-weight sum Algorithm given by text finds only one of these paths

34 Shortest Path in an Weighted Graph Finding the shortest path from vertex A to vertex H

35 Shortest Path in an Weighted Graph The previous graph after finding the shortest path from vertex A to vertex H.

36 Java Interfaces for the ADT Graph Methods in the BasicGraphInterface addVertex addEdge hasEdge isEmpty getNumberOfVertices getNumberOfEdges clear