Ellen Walker CPSC 201 Data Structures Hiram College

Slides:



Advertisements
Similar presentations
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Advertisements

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.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
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.
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.
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.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
COSC 2007 Data Structures II Chapter 14 Graphs III.
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.
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++
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 Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Chapter 20.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Fundamentals, Terminology, Traversal, Algorithms
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Introduction to Graphs
CC 215 Data Structures Graph Searching
Common final examinations
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Comp 245 Data Structures Graphs.
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 22: Elementary Graph Algorithms I
Graphs.
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Outline This topic covers Prim’s algorithm:
Chapter 11 Graphs.
Yan Shi CS/SE 2630 Lecture Notes
CSE 373 Data Structures Lecture 16
ITEC 2620M Introduction to Data Structures
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Chapter 16 1 – Graphs Graph Categories Strong Components
Graphs.
Graphs.
Graphs.
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Graphs.
Lecture 10 Graph Algorithms
GRAPH – Definitions A graph G = (V, E) consists of
GRAPHS.
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:

Ellen Walker CPSC 201 Data Structures Hiram College Graphs Ellen Walker CPSC 201 Data Structures Hiram College

Basic Graph Terminology Edge (link) cycle C A C A No cycle D D B cycle B Vertex (node) Path Note: {A,C} in unordered is like (A,C) + (C A) in ordered Physical layout isn’t relevant, only the connections. In the directed graph, not every node is reachable from every other node, even though the underlying undirected graph is connected E E Directed Graph Undirected Graph V = {A,B,C,D,E} E = {(A,B), (A,E), (A,D), (D,E), (A,C).(C,A)} V = {A,B,C,D,E} E = {{A,B}, {A,C}, {A,D}, {A,E}, {D, E}}

More Terminology C A is adjacent to B, C, D and E (degree is 4) A D is adjacent to A and E ( degree of D is 2) There are 2 connected components (ABCDE and FG) Path BAE is a simple path ; Path BABAC is not. Path ADEA is a cycle A D B F E G

Graph Application Example: Prerequisites CPSC 171 CPSC 240 CPSC 172 CPSC 152 CPSC 386 CPSC 201 CPSC 331 CPSC 361 CPSC 400 CPSC 401

Graph Application Example: Travel Planning 2.5 CLE 1 DEN 1 2 2 ORD PHL 4 4 1.5 SFO 1.5 4 CVG 2 Calculating time cost of connecting flights from CLE to SFO - time in hrs flying time 4 CLT SJC 4

Graphs and Trees Every tree is a graph It is connected It has no cycles Either all links are directed “downward” away from the root, or it is undirected Every connected, no-cycle, undirected graph can be a tree Any node can be chosen as the root

Graph ADT A set of methods Other options might be available Create a new graph (specify # vertices) Iterate through vertices of graph Iterate through neighbors of a vertex Is there an edge between 2 vertices? What is the weight of an edge between 2 vertices? Insert an edge into the graph Other options might be available E.g. add vertices, connect a vertex to another, etc. No built-in graph class in Java

Representing Vertices and Edges Vertex represented as integer Doesn’t allow named vertices Index into an array of names if you need them Edge represented by a class Source vertex, destination vertex, weight (default 1.0) For undirected graph, add each edge in both directions

Graph Representations Adjacency List Each vertex is associated with a list of edges Looks a lot like a hash table; linked lists hanging off an array Adjacency Matrix 2D matrix: M[R][C] = weight of edge from R to C

Adjacency List Example (Directed) 2 1 2 3 4 1 2 3 4 3 4 1 4

Adjacency List Example (Undirected) 2 1 2 3 4 1 2 3 4 3 1 4 3 4

Adjacency Matrix Example 10 2 1 2 3 4 5 8 7 x 10 8 7 5 3 1 7 5 4 Value at row,col = weight from row to col The value “x” means infinity (Double.POSITIVE_INFINITY in Java) Unweighted graph can use boolean: (edge = true)

Graph Traversals vs. Tree Traversals Tree has no cycles; graph can have cycles Mark visited nodes, and don’t repeat them! Tree is connected; graph does not have to be After your traversal is “finished” check to see if there are leftover (not visited) nodes A graph traversal induces a tree structure on the graph The edges that are used to “find” nodes are part of the tree The root of the tree is the node that started the traversal

Breadth-First vs. Depth-First Visit all my immediate neighbors before visiting their neighbors In tree - all children before any grandchildren (level-order traversal) Depth-first Visit first child, first grandchild, … as deep as possible before looking at second child

Breadth First Algorithm For each vertex If the vertex is unseen, offer it to the queue and mark it identified While the queue is not empty Current_vertex = q.poll(); For each neighbor of the current vertex If the neighbor is not yet visited, offer it to the queue and mark it identified Mark current vertex as visited End for End While

Three Kinds of Nodes Unseen (white in your textbook) Nodes that have not yet been seen at all Identified (pale blue in your textbook) Nodes that are currently in the queue Must have at least one visited neighbor Visited (dark blue in your textbook) Nodes that have been completed All neighbors are visited or identified

Breadth-First Search example

Depth First Search Algorithm For each vertex If the vertex is unseen, push it on the stack and mark it identified While the stack is not empty Current_vertex = q.pop(); For each neighbor of the current vertex If the neighbor is not yet visited, push it on the stack and mark it identified Mark current vertex as visited End for End While

Depth First Search Example

Recursive Depth First Search Rec_dfs(current-node){ mark current-node identified for each neighbor of current_node if neighbor is not identified (set parent of neighbor to current node) rec_dfs(neighbor) mark current-node visited }

Comments on Recursive DFS Recursive DFS visits the same nodes, in the same order as Stack DFS Recursive DFS actually uses a stack (Why?) Why is there no recursive BFS?

DFS / BFS analysis Each search visits every node, and considers every neighbor of every node The number of nodes visited is O(V) The number of neigbors considered is O(E) Every vertex is a neighbor for one of its edges Time proportional to V+E But, since E >> V (there cannot be fewer than O(V) edges, but there can be O(V2 )), overall time is O(E) in both cases

Common Graph Algorithms Dijkstra’s algorithm Shortest path from one vertex to all others in a weighted graph If the graph isn’t weighted use BFS. The path you take to the node is by definition the shortest. Prim’s algorithm Finding the spanning tree with the minimum total weight A spanning tree is a subset of (V-1) edges that minimally connects the graph

Dijkstra’s Algorithm The main idea: If the (best possible known) cost from point A to point B is x And there is a connection from B to C that costs y, Then the (best possible known) path from point A to point C through B costs x+y

Keeping Track of Paths We will find all possible distances from a starting node, A Create a table of all other nodes (e.g. B,C,D) Initially, distances are all infinite (unreachable) Now, we’ll visit one node at a time (starting with A) and adding all its links to the table…

Visiting a node Assume you are looking at node A Suppose there is an edge from A to B costing k Let d(A) and d(B) be the distances (in the table) from the starting node to A and B respectively If d(A)+k < d(B), change d(B) to d(A)+k and mark B’s predecessor as A Do the above for all edges of A

Putting it all together Initialize the table Visit the starting node While (not all nodes have been visited) Visit the node that is closest to the starting point When done, the table will contain all shortest distances