Lecture 14: Graph Representation, DFS and Applications
|
|
- Candice Coleen Skinner
- 5 years ago
- Views:
Transcription
1 Lecture 14: Graph Representation, FS and pplications SFO OR LX FW ourtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie
2 Outline Graph Representation dge List djacency List djacency Matrix Graph Traversals epth First Search (FS) pplications of FS 2
3 Graph Representation: dge List Structure Vertex objects stored in unsorted sequence Space O(n) dge Objects stored in unsorted sequence Space O(m) ( ) ach edge object has reference to origin and destination vertex object u v w z v u w z (u,v) (vw) (v,w) (u,w) (w,z) Total space: O(n + m) 3
4 Graph Representation: dge List Structure, Real picture a u c ach vertex object has a back b d pointer reference to the node v w z which references it in the linked list of vertices ( these references are in thick red in the example below ) ach dge object has a back pointer reference to the node which references it in the linked list of edges (thick green references in the example below) u v w z a b c d 4
5 Graph Representation: dge List Structure v u w z (u,v) (v,w) (u,w) (w,z) dvantages: easy to implement Operation Time isadvantages: vertices O(n) inefficient edges O(m) ) incidentdges, aredjacent, endvertices(e), opposite(v,e) O(1) removevertex, incidentdges(v), aredjacent(v,w) O(m) ( ) take O(m), since replace(v,o), replace(e,o), insertvertex(o) O(1) we have to examine the insertdge(u,v),removedge(e) O(1) entire edge removevertex(v) O(m) sequence
6 How to Improve dge List Structure? v w z u v u w z (u,v) (v,w) (u,w) (w,z) The problem with edge list structure is that each vertex does not know which edges are incident on it Solution: put pointers (reference) from each vertex to the edge object incident on this vertex ach vertex can have lots of edges incident on it Thus we need a sequence of incident vertices Thus we need a sequence of incident vertices this sequence is called djacency List, because it s usually implemented as a list
7 Graph Representation: djacency List u v w z (u,v) (v,w) (u,v) (u,w) (u,w) (v,w) (w,z) (w,z) v u w z (u,v) (v,w) (u,w) (w,z) How to remove edge (u,v) efficiently? First remove edge from the edge list, O(1) Then we have to remove pointer to edge (u,v) from the adjacency list of u and the adjacency list of v This will take O((deg(v)+deg(w)), which is not bad, but worse than O(1), complexity of the dge List graph representation Solution: back link each edge (u,v) in the dge Sequence to where it is referenced in the adjacency list of u and v
8 Graph Representation: djacency List Structure u v w z (u,v) (v,w) (u,v) (u,w) (u,w) (v,w) (w,z) (w,z) v u w z (u,v) (v,w) (u,w) (w,z) Space requirement: O(n) ( )for the vertex sequence, O(m) ( )for the edge sequence For vertex v, O(deg(v)) for the adjacency list of v. For all adjacency lists, O(Σ v deg(v) ) = 2m = O(m) by property 1 from the beginning of lecture For the back links O(m) Thus total space is O(n + m) 8
9 djacency List Structure (u,v) (v,w) (u,v) (u,w) (u,w) (v,w) (w,z) v u w z (w,z) (u,v) (v,w) (u,w) (w,z) Operation Time vertices O(n) edges O(m) ) endvertices, opposite O(1) incidentdges(v) O(deg(v)) aredjacent(v,w ) O(min(deg(v),deg(w)) replace O(1) insertvertex,insertdgeinsertdge O(1) removedge(v,w ) O(1) removevertex O((deg(v)) 9 dvantages: More efficient than edge list isadvantages: g More complicated to implement
10 Graph Representation: Traditional djacency Matrix Structure Integer key (index) associated with vertex, indexes from 0 to n 2 dimensional boolean adjacency array M of size n by n M[v,w] = true means that t there is an edge between v and w M[v,w] = false means that there is no edge between een v and w Space requirement: O(n 2 ) Therefore adjacency matrix should be used only for dense graphs graph is dense if m is O(n 2 ), that is it has a lot of edges If m is O(n), then graph is said to be sparse u v w z v u w z v u w z false true true true false false true false true true false true false false true false 10
11 Graph Representation: djacency Matrix Structure dge list structure Integer key (index) associated with vertex v u w z (u,v) (v,w) (u,w) (w,z) 2 adjacency array Reference to edge object for adjacent vertices Null for non-adjacent vertices Space requirement: O(n 2 ) v u w z v u w z 11
12 Graph Representation: djacency Matrix Structure v u w z (u,v) (v,w) (u,w) (w,z) Operation Time vertices O(n) edges O(m) endvertices(e), opposite(e,v) O(1) incidentdges(v) O(n) replace, aredjacent(v,w) O(1) insertdge(v,w), removedge(e) O(1) insertvertex(v), removevertex(v) O(n 2 ) v u w z v u w z 12
13 symptotic Performance n vertices, m edges no parallel edges no self-loops ounds are big-oh dge List djacency List djacency Matrix Space n + m n + m n 2 incidentdges(v) m deg(v) n aredjacent (v, w) m min(deg(v), deg(w)) 1 insertvertex(o) 1 1 n 2 insertdge(v, w, o) removevertex(v) m deg(v) n 2 removedge(e)
14 Summary of Graph ata Structures dge list structure To be used only out of laziness There are no lazy people in this class. Why did we study it? It s a good starting point (as well as part of) for the other data structures djacency list Good performance for any graph Most complicated to implement 2 adjacency array, to be used when: Good choice only for dense graphs (m is O(n 2 ), that is it has a lot of edges) nd used only in case when the set of vertices stays fixed (no vertex insertion or deletion) In cases when the above 2 conditions hold, then the adjacency array is the best choice 14
15 Traversing a Graph v u w z (u,v) (v,w) (u,w) (w,z) The most basic operation on a graph is traversing How do we traverse the graph so that we visit each vertex exactly once and also learn something useful about the graph? ould go through the vertex/edge sequences, but won t learn anything useful, we will just randomly visit vertices/edges To learn anything useful, need to traverse in some natural order for the specific graph lgorithm : Start at a vertex, and visit adjacent vertices However, we have a choice to make when visiting adjacent vertices 1. Go for breadth xplore all edges from the 3: go for breadth current vertex before going to u the next vertex 2. Go for depth Move to the next vertex from v w z the current vertex before finishing exploring current vertex (go deep in the graph) go for depth
16 epth First Search Systematic graph traversal go as deep into the graph as possible an start FS at any vertex v FS(v) Mark all vertices unvisited So that we don t get stuck in a cycle We maintain urrentvertex the vertex we are currently at In the beginning, urrentvertex = v Move away from urrentvertex to an adjacent unvisited vertex Mark urrentvertex as visited urrentvertex = unvisited adjacent vertex of urrentvertex curvertex curvertex
17 epth First Search curvertex curvertex curvertex now we are stuck in a dead end. No more unvisited adjacent vertices from urrentvertex Retrace to the previous vertex Vertex that was urrentvertex previously curvertex
18 epth First Search ontinue switching urrentvertex to the next adjacent unvisited vertex When in dead end, retrace to the previous urrentvertex curvertex curvertex curvertex We can keep a container of previous currentvertex to know which vertex to retrace to This data structure t has to be a stack FILO
19 FS: Non Recursive lgorithm FS(G,v) Mark all vertices UNVISIT onstruct a new empty stack urrentvertex = v while ( urrentvertex t is not null ) Mark urrentvertex VISIT if urrentvertex has adjacent UNVISIT vertex w stack.push(urrentvertex) urrentvertex = w else // ead end, retrace if stack is not empty urrentvertex = stack.pop() else urrentvertex = null
20 xample: FS() curvertex S={ } S={, } curvertex curvertex S={ { } S={,, } curvertex
21 xample ontinued: FS() S={,, } S={,, } curvertex curvertex S={, } S={, } curvertex curvertex
22 xample ontinued: FS() S={, } curvertex S={} curvertex curvertex S={ { } one! stack is empty curvertex VISIT
23 FS: Recursive lgorithm lgorithm FS(G) lgorithm FS(G, v) Input: graph G Input: graph G and a start vertex v of G for all u G.vertices() setlabel(v, VISIT) setlabel(u, UNVISIT) Print(v); //get first vertex in vertex sequence for all e G.incidentdges(v) v = G.vertices().next() w opposite(v,e) ( ) FS(G, v) if getlabel(w) = UNVISIT FS(G, w) 2 done3 done5 done done4 done1 4 Notice that after FS, all edges are divided in 2 groups: Red (solid) discovery edges. These edges are used for discovering new parts of the graph Green(dashed) ( ) back edges. These are not explored It is useful to mark these 2 types of edges when doing FS
24 erivation of FS lgorithm FS(G) Input: graph G for all e G.edges() setlabel(e, UNXPLOR) for all u G.vertices() setlabel(u, UNVISIT) //get first vertex in vertex sequence v = G.vertices().next() FS(G, ( v) ) FS code with labeling of edges into discovery and back edges lgorithm FS(G, v) Input: graph G and a start vertex v of G Output: t: labeling of the edges of G as discovery edges and back edges setlabel(v, VISIT) Print(v); ( ) for all e G.incidentdges(v) w opposite(v,e) if getlabel(w) = UNVISIT setlabel(e, ISOVRY) FS(G, w) else if getlabel(e) = UNXPLOR setlabel(e, K)
25 xample unvisited vertex visited vertex unexplored edge discovery edge back edge 25
26 xample (cont d) 26
27 erivation of FS (cont d) Our current version has only one problem now: it works only for connected graphs lgorithm FS(G) for all e G.edges() setlabel(e, UNXPLOR) for all u G.vertices() setlabel(u, UNVISIT) //get first vertex in vertex sequence v = G.vertices().next() FS(G, v) F G H lgorithm FS(G, v) Input : graph G and a start vertex v of G Output : labeling the edges of G as discovery edges and back edges setlabel(v, VISIT) Print(v); for all e G.incidentdges(v) ( ) w opposite(v,e) if getlabel(w) = UNVISIT setlabel(e, ISOVRY) FS(G, w) else if getlabel(e) = UNXPLOR setlabel(e, K)
28 Final lgorithm for FS To fix, so that t it works on any graph which h is simple Keep calling FS(G, v) from FS(G) for any unvisited v lgorithm FS(G) lgorithm FS(G, v) ) for all e G.edges() Input: graph G and a start vertex v of G setlabel(e, UNXPLOR) Output: labeling the edges of G for all u G.vertices() in the connected component of v setlabel(u, UNVISIT) as discovery edges and back edges for all v G.vertices() setlabel(v, VISIT) if getlabel(v) ( ) = UNVISIT for all e G.incidentdges(v) FS(G, v) w opposite(v,e) ( ) if getlabel(w) = UNVISIT setlabel(e, ISOVRY) FS(G, w) F H else if getlabel(e) = UNXPLOR setlabel(e, K) G no change in FS(G, v) code
29 Properties of FS Property 1 FS(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by FS(G, v) form a spanning tree of the connected component of v 29
30 nalysis of FS ssume adjacency list structure ssume setting/getting a vertex/edge label takes O(1) time FS(G) goes over all graph vertices 2 times and over all edges 1 time takes O(m+n) ( ) time, do not account time spend in FS(G,v). count time spend in FS(G,v) separately FS(Gv) FS(G,v) called 1 time for each vertex one call to FS(G,v) takes 1+deg(v) time all calls to FS(G,v) take time Σ v [1+deg(v)] = Σ v 1+Σ v deg(v) = n+2m FS runs in O(n + m) time (if the graph is represented by the adjacency list structure) lgorithm FS(G) for all e G.edges() setlabel(e, UNXPLOR) for all u G.vertices() setlabel(u, UNVISIT) for all v G.vertices() if getlabel(v) = UNVISIT FS(G, v) lgorithm FS(G, v) setlabel(v, VISIT) for all e G.incidentdges(v) w opposite(v,e) if getlabel(w) = UNVISIT setlabel(e, ISOVRY) FS(G, w) else if getlabel(e) = UNXPLOR setlabel(e, bl( K) )
31 Path Finding we call vertex v active if v is marked VISIT but call to FS(G,v) hasn t terminated yet ctive vertices form a single path In the non-recursive FS version, active vertices are in the stack lgorithm FS(G, v) setlabel(v, VISIT) for all e G.incidentdges(v) w opposite(v,e) if getlabel(w) = UNVISIT setlabel(e, ISOVRY) FS(G, w) ) else if getlabel(e) = UNXPLOR setlabel(e, K) active active done not visited active 31 If we keep track of this path, we can find a path between any 2 vertices, provided there is path between them
32 Path Finding We can specialize the FS algorithm to find a path between two given vertices u and z We call FS(G, u) with u as the start vertex We use a stack S to keep track of the path between the start vertex u and the current vertex Stack holds active vertices Stack S is an instance variable initialized to empty before pathfs is called s soon as destination vertex z is encountered, we return the path as the contents of the stack lgorithm pathfs(g, v, z) setlabel(v, VISIT) S.push(v) if v = z then // found path, return it return Selements() S.elements() for all e G.incidentdges(v) w opposite(v,e) if getlabel(w) ( ) = UNXPLOR result = pathfs(g, w, z) if!(result = null)then return result S.pop(v) return null //did not find path yet 32
33 Path Finding lgorithm pathfs(g, v, z) setlabel(v, VISIT) S.push(v) if v = z then // found path, return it return S.elements() for all e G.incidentdges(v) w opposite(v,e) ( ) if getlabel(w) = UNXPLOR result = pathfs(g, w, z) if!(result = null) ) then return result S.pop(v) return null //did not find path yet S = xample: path between and S =, 33
34 Path Finding lgorithm pathfs(g, v, z) S =, g p (,, ) setlabel(v, VISIT) S.push(v) if v = z then // found path, return it return Selements() S.elements() for all e G.incidentdges(v) w opposite(v,e) if getlabel(w) ( ) = UNXPLOR result = pathfs(g, w, z) if!(result = null)then return result S.pop(v) return null //did not find path yet S =,, 34 S =,,,
35 Path Finding lgorithm pathfs(g, v, z) S =,,, setlabel(v, VISIT) S.push(v) if v = z then // found path, return it return S.elements() for all e G.incidentdges(v) w opposite(v,e) if getlabel(w) = UNXPLOR result = pathfs(g, w, z) if!(result = null)then return result S.pop(v) return null //did not find path yet S =,, 35 S =,,,
36 ycle Finding lgorithm cyclefs(g, v) can specialize FS algorithm to find a simple cycle Mark all edges and vertices as UNXPLOR before call to cyclefs(g,v) use stack S to keep track of the path between the start vertex and the current vertex Stack holds active vertices Stack S is an instance variable initialized to empty before e cyclefs lfsis called s soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w Note: cyclefs(g,v) will only find a cycle in the connected component of v setlabel(v, VISIT) S.push(v) for all e G.incidentdges(v) w opposite(v,e) if getlabel(w) =UNXPLOR setlabel(e, ISOVRY) result = cyclefs(g, w) if!(result = null) then return result else if getlabel(e) = UNXPLOR // found cycle! setlabel(e, K) T new empty stack T.push(w) repeat o S.pop() T.push(o) until o = w return T.elements() S.pop(v) ( ) return null
37 ycle Finding S =,, S = S =,,, S =, w =
38 ycle Finding S =,,, S =,,, w = T = S =,, T =, S =, T =,, S = T =,,, cycle lgorithm cyclefs(g, v) setlabel(v, VISIT) S.push(v) for all e G.incidentdges(v) w opposite(v,e) if getlabel(w) =UNXPLOR setlabel(e, bl( ISOVRY) ) result = cyclefs(g, w) if!(result = null) then return result else if getlabel(e) = UNXPLOR // found cycle! setlabel(e, K) T new empty stack T.push(w) repeat o S.pop() T.push(o) until o = w return T.elements() S.pop(v) return null
39 epth-first Search Summary epth-first search (FS) is a general technique for traversing a graph FS traversal of a graph G Visits all the vertices and edges of G etermines whether G is connected omputes the connected components of G omputes a spanning forest of G 39 FS on a graph with n vertices and m edges takes O(n + m ) time with adjacency list implementation FS can be further extended to solve other graph problems Find and report a path between two given vertices Find a cycle in the graph epth-first search is to graphs what uler tour is to binary trees
Depth-First Search A B D E C
epth-first Search Outline and Reading efinitions (6.1) Subgraph onnectivity Spanning trees and forests epth-first search (6.3.1) lgorithm xample Properties nalysis pplications of FS (6.5) Path finding
More informationData Structures Lecture 14
Fall 2018 Fang Yu Software Security Lab. ept. Management Information Systems, National hengchi University ata Structures Lecture 14 Graphs efinition, Implementation and Traversal Graphs Formally speaking,
More informationGraph ADT. Lecture18: Graph II. Adjacency Matrix. Representation of Graphs. Data Vertices and edges. Methods
Graph T S33: ata Structures (0) Lecture8: Graph II ohyung Han S, POSTH bhhan@postech.ac.kr ata Vertices and edges endvertices(e): an array of the two endvertices of e opposite(v,e): the vertex opposite
More informationBreadth-First Search L 1 L Goodrich, Tamassia, Goldwasser Breadth-First Search 1
readth-irst Search 2013 Goodrich, Tamassia, Goldwasser readth-irst Search 1 readth-irst Search readth-first search (S) is a general technique for traversing a graph S traversal of a graph G Visits all
More informationDFS on Directed Graphs BOS. Outline and Reading ( 6.4) Digraphs. Reachability ( 6.4.1) Directed Acyclic Graphs (DAG s) ( 6.4.4)
S on irected Graphs OS OR JK SO LX W MI irected Graphs S 1.3 1 Outline and Reading ( 6.4) Reachability ( 6.4.1) irected S Strong connectivity irected cyclic Graphs (G s) ( 6.4.4) Topological Sorting irected
More informationDigraphs ( 12.4) Directed Graphs. Digraph Application. Digraph Properties. A digraph is a graph whose edges are all directed.
igraphs ( 12.4) irected Graphs OR OS digraph is a graph whose edges are all directed Short for directed graph pplications one- way streets flights task scheduling irected Graphs 1 irected Graphs 2 igraph
More informationDepth-First Search and Template Patterns
Depth-First Search and Template Patterns A B D E C 1 Outline Template Patterns and Search Depth-first search Algorithm Example Properties Analysis Applications of DFS Path finding Cycle finding CS 4407,
More informationApplications of BFS and DFS
pplications of S and S S all // : PM Some pplications of S and S S o find the shortest path from a vertex s to a vertex v in an unweighted graph o find the length of such a path o construct a S tree/forest
More informationGraphs ORD SFO LAX DFW. Data structures and Algorithms
SFO 143 ORD 337 1743 02 Graphs LAX 1233 DFW Data structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and
More informationCHAPTER 13 GRAPH ALGORITHMS
CHPTER 13 GRPH LGORITHMS SFO LX ORD DFW 1 CKNOWLEDGEMENT: THESE SLIDES RE DPTED FROM SLIDES PROVIDED WITH DT STRUCTURES ND LGORITHMS IN C++, GOODRICH, TMSSI ND MOUNT (WILEY 2004) ND SLIDES FROM JORY DENNY
More informationLecture 16: Directed Graphs
Lecture 16: Directed Graphs ORD JFK BOS SFO LAX DFW MIA Courtesy of Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline Directed Graphs Properties Algorithms for Directed Graphs DFS and
More informationCS 225. April 16 Graph Traversal. Data Structures. Wade Fagen-Ulmschneider
CS 225 Data Structures April 16 Graph Traversal Wade Fagen-Ulmschneider Graph ADT Data: - Vertices - Edges - Some data structure maintaining the structure between vertices and edges. d V W f b e X Y g
More informationGraphs ORD SFO LAX DFW. Graphs 1
Graphs ORD 1843 SFO 802 1743 337 1233 LAX DFW Graphs 1 Outline and Reading Graphs ( 12.1) Definition Applications Terminology Properties ADT Data structures for graphs ( 12.2) Edge list structure Adjacency
More informationDEPTH-FIRST SEARCH A B C D E F G H I J K L M N O P. Graph Traversals. Depth-First Search
PTH-IRST SRH raph Traversals epth-irst Search H I J K L M N O P epth-irst Search 1 xploring a Labyrinth Without etting Lost depth-first search (S) in an undirected graph is like wandering in a labyrinth
More informationGraphs ORD SFO LAX DFW Goodrich, Tamassia Graphs 1
Graphs SFO 33 143 ORD 02 LAX 1233 DFW 2004 Goodrich, Tamassia Graphs 1 What is a Graph? (in computer science, it s not a data plot) General structure for representing positions with an arbitrary connectivity
More informationGraph Terminology and Representations
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Graph Terminology and Representations 1 Graphs A graph is a pair (V, E), where
More informationDirected Graphs BOS Goodrich, Tamassia Directed Graphs 1 ORD JFK SFO DFW LAX MIA
Directed Graphs BOS ORD JFK SFO LAX DFW MIA 2010 Goodrich, Tamassia Directed Graphs 1 Digraphs A digraph is a graph whose edges are all directed Short for directed graph Applications one-way streets flights
More informationAdvanced Data Structures and Algorithms
dvanced ata Structures and lgorithms ssociate Professor r. Raed Ibraheem amed University of uman evelopment, College of Science and Technology Computer Science epartment 2015 2016 epartment of Computer
More informationGRAPH THEORY. What are graphs? Why graphs? Graphs are usually used to represent different elements that are somehow related to each other.
GRPH THEORY Hadrian ng, Kyle See, March 2017 What are graphs? graph G is a pair G = (V, E) where V is a nonempty set of vertices and E is a set of edges e such that e = {a, b where a and b are vertices.
More informationDataStruct 13. Graph Algorithms
2013-2 DataStruct 13. Graph Algorithms Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. December 10, 2013 Advanced Networking Technology Lab.
More information3.1 Basic Definitions and Applications
Graphs hapter hapter Graphs. Basic efinitions and Applications Graph. G = (V, ) n V = nodes. n = edges between pairs of nodes. n aptures pairwise relationship between objects: Undirected graph represents
More informationMinimum Spanning Trees
Minimum Spanning Trees 0 OS SO LX PV OR 0 JK 0 WI 00 W MI 00 Goodrich, Tamassia Minimum Spanning Trees Minimum Spanning Trees Spanning subgraph Subgraph of a graph G containing all the vertices of G Spanning
More informationElementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:
Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered
More informationCS1800: Graph Algorithms (2nd Part) Professor Kevin Gold
S1800: raph lgorithms (2nd Part) Professor Kevin old Summary So ar readth-irst Search (S) and epth-irst Search (S) are two efficient algorithms for finding paths on graphs. S also finds the shortest path.
More informationGraphs ORD SFO LAX DFW
Graphs SFO 337 1843 802 ORD LAX 1233 DFW Graphs A graph is a pair (V, E), where V is a set of odes, called vertices E is a collectio of pairs of vertices, called edges Vertices ad edges are positios ad
More informationGraphs 3/25/14 15:37 SFO LAX Goodrich, Tamassia, Goldwasser Graphs 2
Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Graphs SFO 337 LAX 1843 1743 1233 802 DFW
More informationOutline and Reading. Minimum Spanning Tree. Minimum Spanning Tree. Cycle Property. Minimum Spanning Trees ( 12.7)
Outline and Reading Minimum Spanning Tree PV 1 1 1 1 JK 1 15 SO 11 1 LX 15 Minimum Spanning Trees ( 1.) efinitions crucial fact Prim-Jarnik s lgorithm ( 1..) Kruskal s lgorithm ( 1..1) 111 // :1 PM Minimum
More informationCSI 604 Elementary Graph Algorithms
CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the
More informationAdjacency Matrix Undirected Graph. » Matrix [ p, q ] is 1 (True) if < p, q > is an edge in the graph. Graphs Representation & Paths
djacency Matrix Undirected Graph Row and column indices are vertex numbers Graphs Representation & Paths Mixture of Chapters 9 & 0» Matrix [ p, q ] is (True) if < p, q > is an edge in the graph if < p,
More informationWinter 2016 COMP-250: Introduction to Computer Science. Lecture 16, March 10, 2016
Winter 2016-250: Introduction to Computer Science Lecture 16, March 10, 2016 GRAPHS Definitions Examples The Graph ADT PVD HNL LAX DFW STL FTL What is a Graph? Agraph G = (V,E) is composed of: V: set of
More informationCS2210 Data Structures and Algorithms
S1 ata Structures and Algorithms Lecture 1 : Shortest Paths A 4 1 5 5 3 4 Goodrich, Tamassia Outline Weighted Graphs Shortest Paths Algorithm (ijkstra s) Weighted Graphs ach edge has an associated numerical
More informationAdvanced Algorithms and Computational Models (module A) Lecture no Giacomo Fiumara / 27
Advanced Algorithms and Computational Models (module A) Lecture no. 10 24-10-2014 Giacomo Fiumara giacomo.fiumara@unime.it 2014-2015 1 / 27 Adjacency List For each vertex is maintained a separate list
More informationL10 Graphs. Alice E. Fischer. April Alice E. Fischer L10 Graphs... 1/37 April / 37
L10 Graphs lice. Fischer pril 2016 lice. Fischer L10 Graphs... 1/37 pril 2016 1 / 37 Outline 1 Graphs efinition Graph pplications Graph Representations 2 Graph Implementation 3 Graph lgorithms Sorting
More informationCampus Tour Goodrich, Tamassia. Campus Tour 1
ampus Tour 00 oodrich, Tamassia ampus Tour raph ssignment oals Learn and implement the adjacency matrix structure an Kruskal s minimum spanning tree algorithm Understand and use the decorator pattern and
More information11/26/17. directed graphs. CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10. undirected graphs
directed graphs S 220: iscrete Structures and their pplications collection of vertices and directed edges graphs zybooks chapter 10 collection of vertices and edges undirected graphs What can this represent?
More informationCISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search
IS 320 Introduction to lgorithms all 2014 Lecture 15 epth irst Search 1 Traversing raphs Systematic search of every edge and vertex of graph (directed or undirected) fficiency: each edge is visited no
More information12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees
trees CS 220: Discrete Structures and their Applications A tree is an undirected graph that is connected and has no cycles. Trees Chapter 11 in zybooks rooted trees Rooted trees. Given a tree T, choose
More informationCampus Tour. 1/18/2005 4:08 AM Campus Tour 1
ampus Tour //00 :0 M ampus Tour Outline and Reading Overview of the assignment Review djacency matrix structure (..) Kruskal s MST algorithm (..) Partition T and implementation (..) The decorator pattern
More informationReading. Graph Terminology. Graphs. What are graphs? Varieties. Motivation for Graphs. Reading. CSE 373 Data Structures. Graphs are composed of.
Reading Graph Reading Goodrich and Tamassia, hapter 1 S 7 ata Structures What are graphs? Graphs Yes, this is a graph. Graphs are composed of Nodes (vertices) dges (arcs) node ut we are interested in a
More informationGraphs ADTs and Implementations
Graphs ADTs and Implementations SFO 337 1843 802 ORD LAX 1233 DFW - 1 - Applications of Graphs Ø Electronic circuits cslab1a cslab1b q Printed circuit board math.brown.edu q Integrated circuit Ø Transportation
More informationCS 310 Advanced Data Structures and Algorithms
CS 31 Advanced Data Structures and Algorithms Graphs July 18, 17 Tong Wang UMass Boston CS 31 July 18, 17 1 / 4 Graph Definitions Graph a mathematical construction that describes objects and relations
More informationGraph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship
Graph Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance or cost) path Graph Theory Many problems are mapped
More informationSuggested Study Strategy
Final Exam Thursday, 7 August 2014,19:00 22:00 Closed Book Will cover whole course, with emphasis on material after midterm (hash tables, binary search trees, sorting, graphs) Suggested Study Strategy
More informationUNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,
More informationTrees. Courtesy to Goodrich, Tamassia and Olga Veksler
Lecture 12: BT Trees Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline B-tree Special case of multiway search trees used when data must be stored on the disk, i.e. too large
More informationLecture 9 Graph Traversal
Lecture 9 Graph Traversal Euiseong Seo (euiseong@skku.edu) SWE00: Principles in Programming Spring 0 Euiseong Seo (euiseong@skku.edu) Need for Graphs One of unifying themes of computer science Closely
More informationBreadth First Search. cse2011 section 13.3 of textbook
Breadth irst Search cse section. of textbook Graph raversal (.) Application example Given a graph representation and a vertex s in the graph, find all paths from s to the other vertices. wo common graph
More informationScribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.
Lecture 9 Graphs Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M. Wootters (2017) 1 Graphs A graph is a set of vertices
More informationTopological Sort. CSE 373 Data Structures Lecture 19
Topological Sort S 373 ata Structures Lecture 19 Readings and References Reading Section 9.2 Some slides based on: S 326 by S. Wolfman, 2000 11/27/02 Topological Sort - Lecture 19 2 Topological Sort 142
More informationCS 220: Discrete Structures and their Applications. graphs zybooks chapter 10
CS 220: Discrete Structures and their Applications graphs zybooks chapter 10 directed graphs A collection of vertices and directed edges What can this represent? undirected graphs A collection of vertices
More informationNote. Out of town Thursday afternoon. Willing to meet before 1pm, me if you want to meet then so I know to be in my office
raphs and Trees Note Out of town Thursday afternoon Willing to meet before pm, email me if you want to meet then so I know to be in my office few extra remarks about recursion If you can write it recursively
More informationCSE 100: GRAPH ALGORITHMS
CSE 100: GRAPH ALGORITHMS 2 Graphs: Example A directed graph V5 V = { V = E = { E Path: 3 Graphs: Definitions A directed graph V5 V6 A graph G = (V,E) consists of a set of vertices V and a set of edges
More informationECE 242 Data Structures and Algorithms. Graphs II. Lecture 27. Prof.
242 ata Structures and lgorithms http://www.ecs.umass.edu/~polizzi/teaching/242/ Graphs II Lecture 27 Prof. ric Polizzi Summary Previous Lecture omposed of vertices (nodes) and edges vertex or node edges
More informationGraphs: Topological Sort / Graph Traversals (Chapter 9)
Today s Outline raphs: Topological Sort / raph Traversals (hapter 9) S 373 ata Structures and lgorithms dmin: omework #4 - due Thurs, Nov 8 th at pm Midterm 2, ri Nov 6 raphs Representations Topological
More informationCopyright 2000, Kevin Wayne 1
Chapter 3 - Graphs Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. Directed
More informationIntroduction to Graphs. common/notes/ppt/
Introduction to Graphs http://people.cs.clemson.edu/~pargas/courses/cs212/ common/notes/ppt/ Introduction Graphs are a generalization of trees Nodes or verticies Edges or arcs Two kinds of graphs irected
More informationCS2 Algorithms and Data Structures Note 9
CS2 Algorithms and Data Structures Note 9 Graphs The remaining three lectures of the Algorithms and Data Structures thread will be devoted to graph algorithms. 9.1 Directed and Undirected Graphs A graph
More informationLecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler
Lecture 17: Weighted Graphs, Shortest Paths: Dijkstra s Algorithm A 4 B 7 C 1 D 3 5 3 9 5 E Courtesy of Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Weighted Graphs In a weighted graph, each
More informationToday s Outline CSE 221: Algorithms and Data Structures Graphs (with no Axes to Grind)
Today s Outline S : lgorithms and ata Structures raphs (with no xes to rind) Steve Wolfman 0W Topological Sort: etting to Know raphs with a Sort raph T and raph Representations raph Terminology (a lot
More informationSingle Source, Shortest Path Problem
Lecture : From ijkstra to Prim Today s Topics: ijkstra s Shortest Path lgorithm epth First Search Spanning Trees Minimum Spanning Trees Prim s lgorithm overed in hapter 9 in the textbook Some slides based
More informationOutline. Graphs. Divide and Conquer.
GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.
More informationAnalysis of Algorithms Prof. Karen Daniels
UMass Lowell omputer Science 91.404 nalysis of lgorithms Prof. Karen aniels Spring, 2013 hapter 22: raph lgorithms & rief Introduction to Shortest Paths [Source: ormen et al. textbook except where noted]
More informationSpanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP)
Spanning Tree (015) Lecture1: Graph III ohyung Han S, POSTH bhhan@postech.ac.kr efinition and property Subgraph that contains all vertices of the original graph and is a tree Often, a graph has many different
More informationSELF-BALANCING SEARCH TREES. Chapter 11
SELF-BALANCING SEARCH TREES Chapter 11 Tree Balance and Rotation Section 11.1 Algorithm for Rotation BTNode root = left right = data = 10 BTNode = left right = data = 20 BTNode NULL = left right = NULL
More informationLECTURE 17 GRAPH TRAVERSALS
DATA STRUCTURES AND ALGORITHMS LECTURE 17 GRAPH TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD STRATEGIES Traversals of graphs are also called searches We can use either breadth-first
More informationGraph: representation and traversal
Graph: representation and traversal CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang! Acknowledgement The set of slides have use materials from the following resources Slides for textbook
More informationGraphs and Algorithms
Graphs and Algorithms Graphs are a mathematical concept readily adapted into computer programming. Graphs are not just data structures, that is, they are not solutions to simple data storage problems.
More informationState Transition Graph of 8-Puzzle E W W E W W 3 2 E
rrays. Lists. inary search trees. ash tables. tacks. Queues. tate Transition raph of 8-Puzzle oal: Understand data structures by solving the puzzle problem lementary tructures 4 7 5 6 8 4 6 5 7 8. Trees
More information8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))
DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]
More informationCSE 373: Data Structures and Algorithms. Graph Traversals. Autumn Shrirang (Shri) Mare
SE 373: ata Structures and lgorithms Graph Traversals utumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey hampion, en Jones, dam lank, Michael Lee, Evan Mcarty, Robbie Weber, Whitaker
More informationimplementing the breadth-first search algorithm implementing the depth-first search algorithm
Graph Traversals 1 Graph Traversals representing graphs adjacency matrices and adjacency lists 2 Implementing the Breadth-First and Depth-First Search Algorithms implementing the breadth-first search algorithm
More informationUndirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11
Chapter 3 - Graphs Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. V = {
More informationGraphs. The ultimate data structure. graphs 1
Graphs The ultimate data structure graphs 1 Definition of graph Non-linear data structure consisting of nodes & links between them (like trees in this sense) Unlike trees, graph nodes may be completely
More informationChapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees
msc20 Intro to lgorithms hapter. Greedy lgorithms: Spanning Trees and Minimum Spanning Trees The concept is relevant to connected undirected graphs. Problem: Here is a diagram of a prison for political
More informationGraphs. CS16: Introduction to Data Structures & Algorithms Spring 2018
Graphs CS16: Introduction to Data Structures & Algorithms Spring 2018 Outline What is a Graph Terminology Properties Graph Types Representations Performance BFS/DFS Applications 2 What is a Graph A graph
More informationParallel Pointers: Graphs
Parallel Pointers: Graphs Breadth first Search (BFS) 101 Given a vertex S, what is the distance of all the other vertices from S? Why would this be useful? To find the fastest route through a network.
More informationLecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler
Lecture 11: Multiway and (2,4) Trees 9 2 5 7 10 14 Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline Multiway Seach Tree: a new type of search trees: for ordered d dictionary
More informationTrees. Arash Rafiey. 20 October, 2015
20 October, 2015 Definition Let G = (V, E) be a loop-free undirected graph. G is called a tree if G is connected and contains no cycle. Definition Let G = (V, E) be a loop-free undirected graph. G is called
More informationLecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this
Lecture Notes Array Review An array in C++ is a contiguous block of memory. Since a char is 1 byte, then an array of 5 chars is 5 bytes. For example, if you execute the following C++ code you will allocate
More informationDESIGN AND ANALYSIS OF ALGORITHMS
NPTEL MOOC,JAN-FEB 0 Week, Module DESIGN AND ANALYSIS OF ALGORITHMS Depth first search (DFS) MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE http://www.cmi.ac.in/~madhavan Depth first search Start from
More informationCHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW
SFO ORD CHAPTER 14 GRAPH ALGORITHMS LAX DFW ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) GRAPH
More informationCS302 - Data Structures using C++
CS302 - Data Structures using C++ Topic: Graphs - Introduction Kostas Alexis Terminology In the context of our course, graphs represent relations among data items G = {V,E} A graph is a set of vertices
More informationAn Early Problem in Graph Theory. Clicker Question 1. Konigsberg and the River Pregel
raphs Topic " Hopefully, you've played around a bit with The Oracle of acon at Virginia and discovered how few steps are necessary to link just about anybody who has ever been in a movie to Kevin acon,
More information1. O(log n), because at worst you will only need to downheap the height of the heap.
These are solutions for the practice final. Please note that these solutions are intended to provide you with a basis to check your own answers. In order to get full credit on the exam, you must show all
More informationGraphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}
Graphs and Trees 1 Graphs (simple) graph G = (V, ) consists of V, a nonempty set of vertices and, a set of unordered pairs of distinct vertices called edges. xamples V={,,,,} ={ (,),(,),(,), (,),(,),(,)}
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a
More informationCSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach
CSC 8301- Design and Analysis of Algorithms Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms Brute-Force Approach Brute force is a straightforward approach to solving a problem, usually
More informationcsci 210: Data Structures Graph Traversals
csci 210: Data Structures Graph Traversals Graph traversal (BFS and DFS) G can be undirected or directed We think about coloring each vertex WHITE before we start GRAY after we visit a vertex but before
More informationSpanning Trees 4/19/17. Prelim 2, assignments. Undirected trees
/9/7 Prelim, assignments Prelim is Tuesday. See the course webpage for details. Scope: up to but not including today s lecture. See the review guide for details. Deadline for submitting conflicts has passed.
More informationSpanning Trees. Lecture 22 CS2110 Spring 2017
1 Spanning Trees Lecture 22 CS2110 Spring 2017 1 Prelim 2, assignments Prelim 2 is Tuesday. See the course webpage for details. Scope: up to but not including today s lecture. See the review guide for
More informationData Organization: Creating Order Out Of Chaos
ata Organization: reating Order Out Of haos 3 Non-linear data structures 5-5 Principles of omputation, arnegie Mellon University - ORTIN Trees tree is a hierarchical (usually non-linear) data structure.
More informationCSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms
Chapter 3 CSE 417: Algorithms and Computational Complexity Graphs Reading: 3.1-3.6 Winter 2012 Graphs and Graph Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. Final Examination
University of Illinois at Urbana-Champaign Department of Computer Science Final Examination CS 225 Data Structures and Software Principles Spring 2010 7-10p, Wednesday, May 12 Name: NetID: Lab Section
More informationBreadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms
Breadth irst Search CSE Winter Graph raversal Application examples Given a graph representation and a vertex s in the graph ind all paths from s to the other vertices wo common graph traversal algorithms
More informationCSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT
CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT PROJECT 3 500 Internal Error problems Hopefully all resolved (or close to) P3P1 grades are up (but muted) Leave canvas comment Emails tomorrow End of quarter GRAPHS
More informationToday More about Trees. Introduction to Computers and Programming. Spanning trees. Generic search algorithm. Prim s algorithm Kruskal s algorithm
Introduction to omputers and Programming Prof. I. K. Lundqvist Lecture 8 pril Today More about Trees panning trees Prim s algorithm Kruskal s algorithm eneric search algorithm epth-first search example
More informationA region is each individual area or separate piece of the plane that is divided up by the network.
Math 135 Networks and graphs Key terms Vertex (Vertices) ach point of a graph dge n edge is a segment that connects two vertices. Region region is each individual area or separate piece of the plane that
More informationChapter 9 Graph Algorithms
Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)
More information11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions
Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is
More informationSpanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017
1 Spanning Trees, greedy algorithms Lecture 22 CS2110 Fall 2017 1 We demo A8 Your space ship is on earth, and you hear a distress signal from a distance Planet X. Your job: 1. Rescue stage: Fly your ship
More information