CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Advertisements

Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
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.
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.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
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.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
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.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
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.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
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.
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
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:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Csc 2720 Instructor: Zhuojun Duan
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Heapsort Graphs –traversals

Heaps There is a sorting algorithm based on heaps called HeapSort. The algorithm is simply: –start with n unsorted data items –create a maxHeap (of size n) out of these items --- store it as an array –set i = n -1 swap the root (index 0) and last node (index i)‏ reheapify (downward reheapification) the tree that starts at the root (index 0) and goes to i-1 (do not include the nodes at i and higher in the new heap)‏ –recursively do this until the size of the tree we are heapifying is one (i=0)‏ –The array is now sorted from low to high. Draw on the board.

Graphs Graphs consist of a set of vertices and a set of edges. An edge connects two vertices. Edges can be directed or undirected. Directed graphs' edges are all directed. Undirected graphs' edges are all undirected. Directed graphs are sometimes called digraphs. Two vertices are adjacent if an edge connects them. The degree of a vertex is the number of edges starting at the vertex. Two vertices v1 and v2 are on a path if there are a list of vertices starting at v1 and ending at v2 where each consecutive pair of vertices is adjacent.

Graphs The length of a path is the number of edges in the path. A simple path is one whose edges are all unique. A cycle is a simple path, starting and ending at the same vertex. A vertex is reachable from another vertex if there is a path between them. A graph is connected if all pairs of vertices in the graph have a path between them. Example on the board of a connected and an unconnected graph. A complete graph (aka fully connected graph) is a connected graph where all pairs of vertices in the graph are adjacent. Example on the board.

Graphs Connectivity of a digraph –A digraph is strongly connected if there is a path from any vertex to any other vertex (following the directions of the edges)‏ –A digraph is weakly connected if in its underlying undirected graph, there is a path from any vertex to any other vertex Degree of a vertex in a digraph –In-degree of a vertex is the number of edges entering the vertex –Out-degree of a vertex is the number of edges leaving the vertex

Graphs Edges often have a weight associated with them. An edge's weight is some numeric value. Many things in the real world can be naturally modelled with graphs. Example on the board of using graphs to represent real world problems –e.g. A weighted graph that connect cities (vertices) and the weights of the edges might be length of time to travel between the two cities. –A graph whose vertices represent buildings on campus and edges are sidewalks connecting the buildings.

Graphs Graphs can be represented in programs as many things. Two common ways to represent them are: –adjacency matrix --- each row number r represents a vertex and the value at column c is true if there's an edge from vertex r to vertex c, false otherwise –edge lists store a linked list for each vertex, v i items in the list are those vertices v j for which there's an edge from v i to v j

Graphs Graph traversal –Breadth first search (BFS)‏ Pick a vertex at which to start Visit all of the adjacent vertices to the start vertex Then for each of the adjacent vertices, visit their adjacent vertices And so on until there are no more adjacent vertices Do not visit a vertex more than once –Only vertices that are reachable from the start vertex will be visited --- example on the board. –The order that vertices in a BFS are visited are in increasing order of length of path from starting vertex. –Those that have the same path length from the start vertex can be visited in any order. –Example of BFS on the board.

Graphs Implementation of breadth first search –Need a flag for each vertex to mark it as unvisited, waiting, or visited – so we don't visit vertices more than once. –Keep a queue which will hold the vertices to be visited –Keep a list of vertices as they are visited –BFS algorithm: Mark all vertices as unvisited Initially enqueue a vertex into the queue, mark it as waiting While the queue is not empty –Dequeue a vertex from the queue –Put it in the visited list, mark it as visited –Enqueue all the adjacent vertices that are marked as unvisited to the vertex just dequeued. –Mark the vertices just enqueued as waiting

Graphs Let's see how far we get with implementation of a graph and breadth first search.

Graphs Graph traversal –Depth first search (DFS)‏ Pick a vertex at which to start Visit one of it's adjacent vertices then visit one of that one's adjacent vertices, and so on until there is no unvisited adjacent vertex of the one we're working on. Then backtrack one level and visit another adjacent vertex from that one and repeat. Do this until we're at the start vertex and there's no more unvisited adjacent vertices Do not visit a vertex more than once –Only vertices that are reachable from the start vertex will be visited –Those vertices that are adjacent to a vertex can be visited in any order. –Example of DFS on the board.

Graphs Shortest path algorithms –problem is to find the shortest path from one given vertex to each of the other vertices. –output is a list of paths from given vertex to all other vertices –what real world examples might ever want to find the shortest path?

Graphs Shortest path algorithms –problem is to find the shortest path from one given vertex to each of the other vertices. –output is a list of paths from given vertex to all other vertices –the shortest path could be in terms of path length (number of edges between vertices e.g. a direct flight has path length 1, flights with connecting flights have path length > 1 –the shortest path could be in terms of minimum weight for weighted graphs (example on the board.)‏ e.g. finding the lowest cost flights Dijkstra's algorithm solves this problem

Graphs the shortest path could be in terms of path length (number of edges between vertices) e.g. a direct flight has path length 1, flights with connecting flights have path length > 1 –Initialize all lengths to infinity –Can process the graph in a BFS starting at the given vertex –When visit a node, replace it's length with the current length. –The BFS uses a queue as we saw last time. –Let's implement this.

Graphs the shortest path could be in terms of minimum weight for weighted graphs e.g. finding the lowest cost flights Dijkstra's algorithm solves this problem –It attempts to minimize the weight at each step. Dijkstra's algorithm is a greedy algorithm. That is, its strategy is to locally minimize the weight, hoping that's the best way to get the minimum weight of the whole graph. –Sometimes the local minimum weight is not the correct choice for the overall problem. In that case, the algorithm will still work, but the initial guess was wrong. –Dijkstra's algorithm works in a similar way to BFS but instead of a queue, use a “minimum” priority queue. That is, a priority queue that returns an item whose priority is least among the items in the priority queue. –Let's see an example on the board and come up with pseudocode for this algorithm.