Yan Shi CS/SE 2630 Lecture Notes

Slides:



Advertisements
Similar presentations
CS203 Lecture 15.
Advertisements

Chapter 9: Graphs Shortest Paths
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
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.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Graphs CS3240, L. grewe.
Graph.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Representing and Using Graphs
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
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.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
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.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Section 9.6 Graph Applications. 9.6 Graph Applications Our graph specification does not include traversal operations. We treat traversal as a graph application/algorithm.
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.
Chapter 28 Graphs and Applications
Graphs Representation, BFS, DFS
Lecture 11 Graph Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Cse 373 May 8th – Dijkstras.
C.Eng 213 Data Structures Graphs Fall Section 3.
Lecture 12 Graph Algorithms
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
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.
CSC 172 DATA STRUCTURES.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
Chapter 11 Graphs.
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
COMP171 Depth-First Search.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Depth-First Search CSE 2011 Winter April 2019.
Graphs Chapter 7 Visit for more Learning Resources.
Chapter 10 The Graph ADT.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
CSE 417: Algorithms and Computational Complexity
Graphs.
Graphs.
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graphs.
Lecture 10 Graph Algorithms
Lecture 11 Graph Algorithms
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:

Yan Shi CS/SE 2630 Lecture Notes 14. Graphs Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus Data Structure textbook slides

What is a Graph? Tree: Graph: great for representing hierarchical structures. each node has only one parent. Graph: generalization of a tree a graph G is defined as G = (V,E) V: a set of vertices (nodes) E: a set of edges (connecting the vertices) undirected graph: edges have no direction ( road map ) directed graph: edges have directions ( flight routes )

Undirected Graph Adjacent vertices: Two vertices in a graph that are connected by an edge e.g. A and B, B and C Path: A sequence of vertices that connects two nodes in a graph e.g. Path(A,C) = ?

Directed Graph Root: a vertex with no incoming edge Do we have any root in this graph?

Complete Graph A graph in which every vertex is directly connected to every other vertex How many edges will it have? N vertices: complete directed graph has N(N-1) edges; complete undirected graph has N(N-1)/2 edges.

Clique a clique in an undirected graph is a subset of its vertices such that every two vertices in the subset are connected by an edge. E.g., {A,B,D} is a 3-clique.

Weighted Graph A graph in which each edge carries a value

How to Implement a Graph? If the graph is quite complete, we can use a 2D array  adjacent matrix If the graph is quite sparse, for each vertex, we can use a list to identify outgoing edges  adjacent list

Adjacency Matrix Implementation Adjacency Matrix: for a graph with N nodes, an N by N table that shows the existence (and weights) of all edges in the graph

Adjacent List Implementation Adjacency List: A linked list that identifies all the vertices to which a particular vertex is connected; each vertex has its own adjacency list

Time Complexity of Add/Remove Assume we have an adjacent list implementation There are n vertices and m edges Assume we know the exact location of the vertex/edge, What is the time complexity to Add a vertex Remove a vertex Add an edge Remove an edge O(1) O(m) O(1) O(1)

Common Graph Algorithms Depth-first search traversal algorithm: Visit all the nodes in a branch to its deepest point before moving up similar to post-order traversal of a tree stack-based   Breadth-first search traversal algorithm: Visit all the nodes on one level before going to the next level similar to pre-order traversal of a tree queue-based Shortest-path algorithm: An algorithm that displays the shortest path from a designated starting node to every other node in the graph

Depth First Search Question: can I get to vertex B from vertex A? DFS Algorithm: go as far as possible first Time Complexity: O( n + m ) found = false stack.push(A) visited = empty set while !found && !stack.empty() vertex = stack.pop() found = ( vertex == B ) if ( !found ) for each v adjacent to vertex and not marked mark v stack.push(v) if found write "path exists!"

Austin to Chicago Austin

Austin to Chicago Houston Dallas

Austin to Chicago Atlanta Dallas

Austin to Chicago Washington Dallas

Austin to Chicago Dallas

Austin to Chicago Denver Chicago

Austin to Chicago Chicago

Austin to Chicago Chicago Found

Depth First Traversal from Austin Austin  Houston  Atlanta  Washington  Dallas  Denver  Chicago

Breadth First Search try all adjacent nodes first Algorithm: found = false queue.enqueue(A) visited = empty set while !found && !queue.empty() vertex = queue.dequeue() found = ( vertex == B ) if ( !found ) for each v adjacent to vertex and not marked mark v queue.enqueue(v) if found write "path exists!"

Austin to Washington Austin

Austin to Washington Houston Dallas

Austin to Washington Denver Chicago Houston

Austin to Washington Atlanta Denver Chicago

Austin to Washington Atlanta Denver

Austin to Washington Atlanta

Austin to Washington Washington

Austin to Washington

Breath First Traversal from Austin Austin  Dallas  Houston  Chicago  Denver  Atlanta  Washington

Single Source Shortest Path Given: Weighted directed graph G, single sources s. Goal: Find shortest paths from s to every other vertex Very similar to depth and breadth first search except: use a minimum priority queue instead of a stack or queue there is no destination: stop only when there are no more cities in the process

Dijkstra’s algorithm Dijkstra’s algorithm: http://en.wikipedia.org/wiki/Dijkstra's_algorithm G = (V,E} S = {vertices whose shortest paths from the source is determined} di = best estimate of shortest path to vertex i pi = predecessors Initialize di and pi, Set S to empty, While there are still vertices in V-S, Sort the vertices in V-S according to the current best estimate of their distance from the source, Add u, the closest vertex in V-S, to S, Update all the vertices still in V-S connected to u to a better estimation if possible

Dijkstra’s algorithm example 1 2 3 4 5 6 7 10 8

Dijkstra’s algorithm example 0+7=7 1 2 3 4 5 6 7 10 8 0+10=10

Dijkstra’s algorithm example 0+7=7 7+10=17 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13

Dijkstra’s algorithm example 0+7=7 7+10=17 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13

Dijkstra’s algorithm example 0+7=7 7+6+1=14 7+6+8=21 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13

Dijkstra’s algorithm example 0+7=7 7+6+1=14 7+6+1+2=16 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13

Dijkstra’s algorithm example 0+7=7 7+6+1=14 7+6+1+2=16 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13