1. Graph and Representation

Size: px
Start display at page:

Download "1. Graph and Representation"

Transcription

1 Chapter Graphs Tree Root Direction: parent-child relationships No cycles Graph Vertices + Edges No tree restrictions Examples World Wide Web Maps. Graph and Representation A graph G: a pair (V, E) vertices V edges E, each pair of vertices (v i, v j ) for v i, v j V.

2 Graph: definition (trivia) Are these graphs? A single vertex: v 0 Yes. V = {v 0, E = {. A single vertex with a self-loop edge: v 0 Yes. V = {v 0, E = {e 0. Graph: edge An edge e is directed if defined as an ordered pair of vertices (v i, v j ). A directed edge is denoted by an arrow v i v j. V i is called the source, and v j is called the destination of the edge. The number of incoming edges to a vertex is called the in-degree, and the number of outgoing edges is called the out-degree. An undirected edge e` between v i and v j is equivalent to two directed edges v i v j and v j v i.

3 Graph: path A path is a sequence v, v,,v N such that (v i, v i+ ) E for i=,,,n-. The length of a path: the number of edges on the path, i.e., N-. The path is a cycle if v v N. The path is simple if all v i, i=,,,n-, are distinct. (v v N is allowed.) Given a path v, v, v i-, v i, v i+,,v N, v, v, v i- are v i s predecessors v i+,,v N are v i s successors. 5 Graph types (/) A graph G is directed if each edge is directed. (a.k.a., digraph) A graph is weighted if each edge has a weight, i.e., E = { (v i, v j, w). A graph is acyclic if there is no cycle in any path. A graph is connected if there exists a path between any v i, v j V, i j. A directed graph is weakly connected if connected only if the direction of edges is ignored. A network is a connected weighted directed graph.

4 Graph types (/) Connected graph Weakly connected graph 7 Graph types (/) An undirected graph is completely connected if every pair of vertices has an edge connecting them. An undirected graph is minimally connected if each pair of vertices has only one path connecting them. This graph is also called a spanning tree. It has exactly N- edges for a graph of N vertices. 8

5 Graph: types (/) Completely connected graph E = V ( V -)/ = ( V ) Minimally connected graph (i.e., spanning tree) E = V - = ( V ) ( V : the number of vertices ) Graph representation: data structures v i, v j,, i j, are adjacent if there exists an edge (v i, v j ) between them. Adjacency matrix: a -dimensional array Adjacency list: an array of linked list. Preferred for a sparse graph. ( Sparser means fewer edges.) 0 5

6 Graph representation: adjacency list [] [] [5] [7] [] [] [] [] [0] [] [] [0] The order of nodes in the linked list does not matter. Graph representation: adjacency matrix Side note: We say a vertex is connected to itself only through an explicit self-loop edge, so assign 0 to the diagonal elements. to from 0 5 7

7 Graph representation: class exercise Represent the following undirected graph using the adjacency list and the adjacency matrix For an undirected graph, the number of nodes in the adjacency list and the number of bit s in the adjacency matrix are twice those for the directed graph. Graph representation: running time and storage space Adj. Matrix Adj. List Time: Adjacent(v i,v j ) ( ) O( V ) Process all edges ( V ) ( V + E ) Storage space: ( V ) ( V + E ) Adjacent(v i, v j ): O( V ) for the adjacency list because for accessing the i-th array element for v i and V - in the worst case to find v j. Example of the worst case is a completely connected graph. Storage space: ( V + E ) for the adjacency list, where V is the array size and E is for the total number of linked nodes. 7

8 . Graph traversal Algorithms Implementations Running time 5 Graph traversal: algorithms Breadth-first search (or traversal) Depth-first search (or traversal) (We consider connected graphs here.) 8

9 Graph traversal: side notes (/) Unlike tree traversal, there may be more than one path to reach a vertex. So, each time the next node is visited, it should be checked whether it has already been visited. 7 Graph traversal: side notes (/) In addition to breadth-first and depthfirst, there is also the priority-first search. In this search, each vertex or edge has a priority assigned, and the vertices are traversed in the order of the priority. 8

10 Breadth-first search void BFS(vertex v) { FIFO<vertex> queue; queue.put(v); while (queue is not empty) { w = queue.get(); if w has not been visited { visit w; mark w as visited; for each vertex t adjacent to w if t has not been visited queue.put(t); Depth-first search void DFS(vertex v) { LIFO<vertex> stack; stack.push(v); while (stack is not empty) { w = stack.pop(); if w has not been visited { visit w; mark w as visited; for each vertex t adjacent to w if t has not been visited stack.push(t); The DFS algorithm is the same as the BFS algorithm except that it uses a LIFO queue (stack) instead of a FIFO queue. 0 0

11 Graph traversal: recursive algorithms void DFS(vertex v) { visit v; mark v as visited; for each t adjacent to v if t has not been visited then DFS(t); Like the non-recursive counterpart, this recursive function should use a global array to keep track of the vertices visited. void BFS(vertex v) { // Well, it won t be as straightforward as DFS. // But, please try it if you will. Best-first Search: with Domain Knowledge

12 Exercise: BFS and DFS Breadth-first search and depth-first search starting with vertex : List the orders of vertices to be visited on (a). (a) (b) Graph traversal: implementation Both BFS and DFS algorithms can be implemented using either adjacency list or adjacency matrix as the data structure for graph.

13 Graph traversal implementation: example (/): BFS & DFS with Adj. list visited: for keeping track of the vertices visited 5 Graph traversal implementation: example (/) Exercise: Given the adjacency list, start from the vertex 0 and list the vertices visited during the BFS and DFS, respectively. Mark the visited array elements during the traversal. Answer: Vertices visited by BFS: 0,,,, 5,,. Vertices visited by DFS: 0,,,,,, 5.

14 Graph traversal: running time O( E + V ) for both BFS and DFS, if adjacency lists are used. Why? (See the next slides: running time observations.) Note: O( E + V ) ~= O( V ) for a dense graph Consider a completely connected graph, for which E = V ( V -)/ = Θ( V ). O( E + V ) ~= O( V ) for a sparse graph Consider a minimum spanning tree, for which E = V - = Θ( V ). 7 Graph traversal: running time observations (/) void BFS(vertex v) { // The same analysis for DFS. FIFO<vertex> queue; queue.put(v); while (queue is not empty) { w = queue.get(); if w has not been visited { visit w; mark w as visited; for each vertex t adjacent to w if t has not been visited queue.put(t); How many iterations? (See the next slide for observations.) 8

15 Graph traversal: running time observations (/) Part iterates E times. Each edge is traversed exactly once. It iterates Θ( V ) times for a dense graph and Θ( V ) times for a sparse graph as Θ( E ) = Θ( V ) for a dense graph Θ( E ) = Θ( V ) for a sparse graph. Part iterates V times as it is executed only once for each vertex. In Part, queue.put(t) iterates Θ( E ) times the total number of edges adjacent to each vertex is E for an undirected graph or E for a directed graph.. Topological sort Given a directed acyclic graph(dag) G = (V, E), a topological sort orders the vertices in a topological order so that: for any v i,v j V (i j), v j appears after v i in the ordering if there exists a path from v i to v j. 0 5

16 Topological sort: algorithm Topological_sort (graph G) { repeat { find a vertex v with no incoming edge in G; break if not found; output v as the next vertex in the sorted order; remove the vertex v and its outgoing edges from G; If G is not empty then error; Q. What does this error mean? A. There is a cycle in the graph, as every vertex in the remaining G has an incoming edge. Topological sort: example Topologically sort the vertices in the following DAG. v v v v v 5 v v 7 The resulting topological order is either <v, v, v 5, v, v, v 7, v > or <v, v, v 5, v, v 7, v, v >.

17 Topological sort: algorithm (pseudo-code) Add an in-degree field and a topological_order field in each vertex. Use a FIFO queue to keep the vertices with zero in-degree. We may use a stack (i.e., LIFO queue) instead. Topological_sort(graph G, FIFO queue) { counter = 0; for each vertex v in G if (v.indegree == 0) then queue.put(v); while queue is not empty { v = queue.get(); v.topological_order = ++ counter; for each vertex w adjacent to v { w.indegree = w.indegree ; if (w.indegree == 0) then queue.put(w); Topological sort: example G: Use an adjacency list (with in-degree and topological order stored in the array) for the graph G. v v v in- topological deg order v v v v v v v v v 5 5 v v v v 7 v v v v 7 v 5 v v 7 v Queue: v 7 v The resulting topological order is either <v, v, v 5, v, v, v 7, v > or <v, v, v 5, v, v 7, v, v >. 7

18 Topological sort: exercise Use an adjacency list to represent the graph. c f h a b d g i j e The result varies depending on the order of nodes in the adjacency list. 5 Topological sort: running time O( V + E ) Topological_sort(graph G, FIFO queue) { counter = 0; for each vertex v in G if (v.indegree == 0) then queue.put(v); while queue is not empty { v = queue.get(); v.topological_order = ++ counter; for each vertex w adjacent to v { w.indegree = w.indegree ; if (w.indegree == 0) then queue.put(w); Total O( V ) times Total V times Total E times 8

19 . Shortest path problems a.k.a. network Given a connected weighted directed graph, Single source single destination shortest path problem: Given a source vertex and a destination vertex, find a shortest path between them. Single source shortest paths problem: Given a source vertex, find a shortest path from it to each of the other vertices. (We consider this problem here.) All pairs shortest paths problem: Find a shortest path between each pair of vertices. 7 Source: Clifford Shaffer Shortest path: examples Shortest(A,E) Shortest(A,x) where x {B,C,D,E Shortest(x,y) where x, y {A,B,C,D,E and x y A 0 B 0 C 5 5 D E 8

20 Shortest path tree: definition Given a connected weighted directed graph, a shortest path tree (SPT) from a vertex s is a directed tree rooted at s each tree path from s to another vertex vis the shortest among all possible paths from s to v. Shortest path tree: example Build a shortest path tree from vertex A. A E 7 5 C D B F 0 0

21 Dijkstra s algorithm: concept Solves the single source shortest paths problem for a connected weighted directed graph with nonnegative weights. Is a greedy algorithm. Chooses the next step with the immediate biggest benefit (or, smallest cost). May end up finding only a local optimum. Dijkstra s algorithm: outline (/). Include the source in the SPT.. Build the SPT one edge at a time. Take next an edge that gives the shortest path from the source to a vertex not yet in the SPT. (I.e., add vertices to the SPT in order of their distance (through the SPT) from the start vertex.) (Source: Robert Sedgewick)

22 Dijkstra s algorithm: outline (/) A bookkeeping table for each vertex: Whether the vertex has been included in the SPT. The distance from the source to the vertex. The predecessor in the path from the source to the vertex. Two kinds of data structures for the bookkeeping table Bk. Array<VtxInfo> Bk[NUM_VERTICES]. Priority queue <VtxInfo> Bk. where VtxInfo has three fields: { Bool included; float distance; Vertex predecessor;. Dijkstra s algorithm: pseudo code void Dijkstra(Vertex s) { for each vertex t { // initialize the vertex information t.included = false; t.distance = ; t.predecessor = null; s.distance = 0; repeat { Find the vertex v with the smallest distance from the source s among those not included in the SPT yet; If not found break; // If all vertices have been included then we re done. else v.included = true; // mark v as included (i.e., chosen) for each vertex w adjacent to v { if (!w.included and (v.distance + weight(v,w) < w.distance)) { decrease w.distance to v.distance+weight(v,w); w.predecessor = v; // add (v,w) as a candidate If w had a different predecessor, u, then w.predecessor = v replaces it with v. That is, discard the candidate (u,w) and add a new candidate (v,w).

23 Dijkstra s algorithm: example (/) Start vertex: A Ack: Clifford Shaffer for the graph chosen candidate discarded A C B D F E 7 5 A C B D F E E A C B D F A C B D F E A C B D F E A C B D F E Dijkstra s algorithm: example (/) A C B D F E A C B D F E

24 Dijkstra s algorithm: analysis Repeated V times Repeated V times Repeated O( E ) times void Dijkstra(Vertex s) { for each vertex t { // initialize the vertex information t.included = false; t.distance = ; t.predecessor = null; s.distance = 0; repeat { Find the vertex v with the smallest distance from the source s among those not included in the SPT yet; If not found break; // If all vertices have been included then we re done. else v.included = true; // mark v as included (i.e., chosen) for each vertex w adjacent to v { if (!w.included and (v.distance + weight(v,w) < w.distance)) { decrease w.distance to v.distance+weight(v,w); w.predecessor = v; // add (v,w) as a candidate Repeated E times The running time varies depending on the implementation of the blue lines. 7 Dijkstra s algorithm implementation using an array void Dijkstra(int s) { for each vertex Bk[t] (t [0, V -]) { // initialize the vertex information Bk[t].included = false; Bk[t].distance = ; Bk[t].predecessor = null; // Θ() Bk[s].distance = 0; repeat { min_distance = ; for each vertex Bk[v] (v [0, V -]) { if (!Bk[v].included && Bk[v].distance < min_distance) { min_distance = Bk[v].distance; min_v = Bk[v]; If (min_distance = ) break; // not found else min_v.included = true; // mark v as included. for each vertex Bk[w] adjacent to min_v { ( V ) if (!Bk[w].included and (min_v.distance + weight(min_v, w) < Bk[w].distance)) { Bk[w].distance = min_v.distance+weight(min_v,w); // Θ() Bk[w].predecessor = min_v; 8

25 Dijkstra s algorithm implementation using an array: running time O( V + V V + E ) = O( V + E ) = O( V ) ( V - E ( V -) V /) = O( E ) if the graph is dense O( E ) if the graph is sparse Using a priority queue is a better implementation (not covered in this course, with O( E log E ) ) if the graph is sparse. Chapter Graphs: Summary. Definitions and Representation (Section.). Graph traversal algorithms (Section.). Topological sort algorithm (Section.). The shortest path problems (Section.) 5. Other Topics (Not Covered in Class) Network flow algorithms (Section.) Minimum spanning tree algorithms (Section.5) 50 5

Basic Graph Definitions

Basic Graph Definitions CMSC 341 Graphs Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge is a pair (v,w) where v, w V. V and E are sets, so each vertex

More information

Graphs & Digraphs Tuesday, November 06, 2007

Graphs & Digraphs Tuesday, November 06, 2007 Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements

More information

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,

More information

Info 2950, Lecture 16

Info 2950, Lecture 16 Info 2950, Lecture 16 28 Mar 2017 Prob Set 5: due Fri night 31 Mar Breadth first search (BFS) and Depth First Search (DFS) Must have an ordering on the vertices of the graph. In most examples here, the

More information

Algorithm Design and Analysis

Algorithm 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 information

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1 Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset

More information

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies: UNIT 5 CSE 103 - Unit V- Graph GRAPH Graph is another important non-linear data structure. In tree Structure, there is a hierarchical relationship between, parent and children that is one-to-many relationship.

More information

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value 1 Possible implementations Sorted linear implementations o Appropriate if

More information

CS 310 Advanced Data Structures and Algorithms

CS 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 information

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list UNIT-4 Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path and Transitive closure, Topological sort. Sets: Representation - Operations on sets Applications. 1. Name

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 5 Exploring graphs Adam Smith 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Puzzles Suppose an undirected graph G is connected.

More information

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1 Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 8: Graphs Jan Křetínský Winter 2017/18 Chapter 8: Graphs, Winter 2017/18 1 Graphs Definition (Graph) A graph G = (V, E) consists of a set V of vertices (nodes) and a set

More information

CS 61B Data Structures and Programming Methodology. Aug 5, 2008 David Sun

CS 61B Data Structures and Programming Methodology. Aug 5, 2008 David Sun CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun Graph Traversal Many algorithms on graphs depend on traversing all or some subset of the verfces. Unlike tree traversals, straight

More information

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity. 1. T F: Consider a directed graph G = (V, E) and a vertex s V. Suppose that for all v V, there exists a directed path in G from s to v. Suppose that a DFS is run on G, starting from s. Then, true or false:

More information

Elementary 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: 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 information

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100 Minimum Spanning Trees Prim s and Kruskal CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:

More information

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College Laura Toma Algorithms (csci2200), Bowdoin College Undirected graphs Concepts: connectivity, connected components paths (undirected) cycles Basic problems, given undirected graph G: is G connected how many

More information

SELF-BALANCING SEARCH TREES. Chapter 11

SELF-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 information

CSE 100: GRAPH ALGORITHMS

CSE 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 information

Lecture 9 Graph Traversal

Lecture 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 information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

Chapter 9 Graph Algorithms

Chapter 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 information

Outline. Graphs. Divide and Conquer.

Outline. 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 information

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes Graphs Graphs A graph is a data structure consisting of nodes (or vertices) and edges An edge is a connection between two nodes A D B E C Nodes: A, B, C, D, E Edges: (A, B), (A, D), (D, E), (E, C) Nodes

More information

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

11/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 information

Topic 12: Elementary Graph Algorithms

Topic 12: Elementary Graph Algorithms Topic 12: Elementary Graph Algorithms Pierre Flener Lars-enrik Eriksson (version of 2013-02-10) Different kinds of graphs List Tree A B C A B C Directed Acyclic Graph (DAG) D A B C D E Directed Graph (Digraph)

More information

GRAPHS Lecture 17 CS2110 Spring 2014

GRAPHS Lecture 17 CS2110 Spring 2014 GRAPHS Lecture 17 CS2110 Spring 2014 These are not Graphs 2...not the kind we mean, anyway These are Graphs 3 K 5 K 3,3 = Applications of Graphs 4 Communication networks The internet is a huge graph Routing

More information

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo

More information

Algorithm Design (8) Graph Algorithms 1/2

Algorithm Design (8) Graph Algorithms 1/2 Graph Algorithm Design (8) Graph Algorithms / Graph:, : A finite set of vertices (or nodes) : A finite set of edges (or arcs or branches) each of which connect two vertices Takashi Chikayama School of

More information

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131 Graphs Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - Faner 3131 1 Outline Introduction to Graphs Graph Traversals Finding a

More information

Graphs. The ultimate data structure. graphs 1

Graphs. 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 information

CSI 604 Elementary Graph Algorithms

CSI 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 information

Homework Assignment #3 Graph

Homework Assignment #3 Graph CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 Graph Some of the problems are adapted from problems in the book Introduction to Algorithms by Cormen, Leiserson and Rivest, and some are

More information

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp Charles Lin Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp Adjacency Matrix bool way[100][100]; cin >> i >> j; way[i][j] = true;

More information

Review: Graph Theory and Representation

Review: Graph Theory and Representation Review: Graph Theory and Representation Graph Algorithms Graphs and Theorems about Graphs Graph implementation Graph Algorithms Shortest paths Minimum spanning tree What can graphs model? Cost of wiring

More information

Konigsberg Bridge Problem

Konigsberg Bridge Problem Graphs Konigsberg Bridge Problem c C d g A Kneiphof e D a B b f c A C d e g D a b f B Euler s Graph Degree of a vertex: the number of edges incident to it Euler showed that there is a walk starting at

More information

CS302 - Data Structures using C++

CS302 - 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 information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1 Measuring Running Time Focus on scalability: parameterize the running time

More information

CS 491 CAP Introduction to Graphs and Search

CS 491 CAP Introduction to Graphs and Search CS 491 CAP Introduction to Graphs and Search Jingbo Shang University of Illinois at Urbana-Champaign Sep 9, 2016 Outline Graphs Adjacency Matrix vs. Adjacency List Special Graphs Depth-first and Breadth-first

More information

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017 CSC 172 Data Structures and Algorithms Lecture 24 Fall 2017 ANALYSIS OF DIJKSTRA S ALGORITHM CSC 172, Fall 2017 Implementation and analysis The initialization requires Q( V ) memory and run time We iterate

More information

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph. Vertex. edge. Directed Graph. Undirected Graph Module : Graphs Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS E-mail: natarajan.meghanathan@jsums.edu Graph Graph is a data structure that is a collection

More information

Introduction to Algorithms

Introduction to Algorithms 6.006- Introduction to Algorithms Lecture 13 Prof. Constantinos Daskalakis CLRS 22.4-22.5 Graphs G=(V,E) V a set of vertices Usually number denoted by n E VxV a set of edges (pairs of vertices) Usually

More information

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation) MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review DFS, BFS Topological Sort MA/CSSE 473 Day 12 Questions? Interpolation Search Insertion sort analysis Depth first Search Breadth first

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers -6 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers Carnegie Mellon University Africa

More information

Graph: representation and traversal

Graph: 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 information

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018 COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018 Name: ID: Answer the questions directly on the exam pages. Show all your work for each question. More detail including comments

More information

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles.

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles. Today s Outline Graphs Chapter 9 in Weiss CSE 6 Data Structures Ruth Anderson /6/00 Announcements Written Homework #6 due NOW Project Code due Mon March by pm Project Benchmarking & Written due Thurs March

More information

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II Anna University 2 & 16 Mark Questions & Answers Year / Semester: II / III

More information

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph Graphs Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs start Birmingham 60 Rugby fill pan with water add salt to water take egg from fridge

More information

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

ROOT: A node which doesn't have a parent. In the above tree. The Root is A. 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 T 1, T 2...T k, each of whose roots are connected

More information

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006 Trees and Graphs Basic Definitions Tree: Any connected, acyclic graph G = (V,E) E = V -1 n-ary Tree: Tree s/t all vertices of degree n+1 A root has degree n Binary Search Tree: A binary tree such that

More information

COMP 251 Winter 2017 Online quizzes with answers

COMP 251 Winter 2017 Online quizzes with answers COMP 251 Winter 2017 Online quizzes with answers Open Addressing (2) Which of the following assertions are true about open address tables? A. You cannot store more records than the total number of slots

More information

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs 5 3 2 4 1 0 2 Graphs A graph G = (V, E) Where V is a set of vertices E is a set of edges connecting vertex pairs Example: V = {0, 1, 2, 3, 4, 5} E = {(0, 1),

More information

Graphs: basic concepts and algorithms

Graphs: basic concepts and algorithms : basic concepts and algorithms Topics covered by this lecture: - Reminder Trees Trees (in-order,post-order,pre-order) s (BFS, DFS) Denitions: Reminder Directed graph (digraph): G = (V, E), V - vertex

More information

Introduction to Graphs. common/notes/ppt/

Introduction 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 information

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering CS781 Lecture 2 January 13, 2010 Graph Traversals, Search, and Ordering Review of Lecture 1 Notions of Algorithm Scalability Worst-Case and Average-Case Analysis Asymptotic Growth Rates: Big-Oh Prototypical

More information

CS/COE

CS/COE CS/COE 151 www.cs.pitt.edu/~lipschultz/cs151/ Graphs 5 3 2 4 1 Graphs A graph G = (V, E) Where V is a set of vertices E is a set of edges connecting vertex pairs Example: V = {, 1, 2, 3, 4, 5} E = {(,

More information

GRAPHS Lecture 19 CS2110 Spring 2013

GRAPHS Lecture 19 CS2110 Spring 2013 GRAPHS Lecture 19 CS2110 Spring 2013 Announcements 2 Prelim 2: Two and a half weeks from now Tuesday, April16, 7:30-9pm, Statler Exam conflicts? We need to hear about them and can arrange a makeup It would

More information

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Graph 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 information

CS 561, Lecture 10. Jared Saia University of New Mexico

CS 561, Lecture 10. Jared Saia University of New Mexico CS 561, Lecture 10 Jared Saia University of New Mexico Today s Outline The path that can be trodden is not the enduring and unchanging Path. The name that can be named is not the enduring and unchanging

More information

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CSE 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 information

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu. 17CA 104DATA STRUCTURES Academic Year : 018-019 Programme : MCA Year / Semester : I / I Question Bank Course Coordinator: Mrs. C.Mallika Course Objectives The student should be able to 1. To understand

More information

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graph Algorithms: Chapters Part 1: Introductory graph concepts UMass Lowell Computer Science 91.503 Algorithms Dr. Haim Levkowitz Fall, 2007 Graph Algorithms: Chapters 22-25 Part 1: Introductory graph concepts 1 91.404 Graph Review Elementary Graph Algorithms Minimum

More information

csci 210: Data Structures Graph Traversals

csci 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 information

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

( ) ( ) C.  1 n. ( ) $ f n. ( ) B.  log( n! ) ( ) and that you already know ( ) ( )  % g( n) ( )  #& CSE 0 Name Test Summer 008 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time for the following code is in which set? for (i=0; i

More information

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. UNIT III TREES A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. Tree: A tree is a finite set of one or more nodes such that, there

More information

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF

More information

CS24 Week 8 Lecture 1

CS24 Week 8 Lecture 1 CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 04 / 06 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Graphs Definition Terminology two ways to represent edges in implementation traversals

More information

CSE 373 Final Exam 3/14/06 Sample Solution

CSE 373 Final Exam 3/14/06 Sample Solution Question 1. (6 points) A priority queue is a data structure that supports storing a set of values, each of which has an associated key. Each key-value pair is an entry in the priority queue. The basic

More information

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1 CS200: Graphs Prichard Ch. 14 Rosen Ch. 10 CS200 - Graphs 1 Graphs A collection of nodes and edges What can this represent? n A computer network n Abstraction of a map n Social network CS200 - Graphs 2

More information

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Scribes: 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 information

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description Plan CMPSCI 311: Introduction to Algorithms Akshay Krishnamurthy and Andrew McGregor University of Massachusetts Review: Breadth First Search Depth First Search Traversal Implementation and Running Time

More information

CMSC 132: Object-Oriented Programming II. Shortest Paths

CMSC 132: Object-Oriented Programming II. Shortest Paths CMSC 13: Object-Oriented Programming II Shortest Paths 1 Quiz 1 One advantage of adjacency list representation over adjacency matrix representation of a graph is that in adjacency list representation,

More information

Graph Traversals. CS200 - Graphs 1

Graph Traversals. CS200 - Graphs 1 Graph Traversals CS200 - Graphs 1 Tree traversal reminder A Pre order A B D G H C E F I B C In order G D H B A E C F I D E F Post order G H D B E I F C A G H I Level order A B C D E F G H I Connected Components

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 7 Greedy Graph Algorithms Topological sort Shortest paths Adam Smith The (Algorithm) Design Process 1. Work out the answer for some examples. Look for a general principle

More information

Basic Graph Algorithms (CLRS B.4-B.5, )

Basic Graph Algorithms (CLRS B.4-B.5, ) Basic Graph Algorithms (CLRS B.-B.,.-.) Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices V and a finite set of edges E. Directed graphs: E is a set of ordered pairs of vertices

More information

TIE Graph algorithms

TIE Graph algorithms TIE-20106 1 1 Graph algorithms This chapter discusses the data structure that is a collection of points (called nodes or vertices) and connections between them (called edges or arcs) a graph. The common

More information

Graphs Data Structures

Graphs Data Structures Graphs Data Structures Introduction We looked previously at the binary tree data structure, which provides a useful way of storing data for efficient searching. In a binary tree, each node can have up

More information

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

Applications of BDF and DFS

Applications of BDF and DFS January 14, 2016 1 Deciding whether a graph is bipartite using BFS. 2 Finding connected components of a graph (both BFS and DFS) works. 3 Deciding whether a digraph is strongly connected. 4 Finding cut

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II DIRECTED GRAPHS Graphs slides are modified from COS 126 slides of Dr. Robert Sedgewick. 1 Directed graphs Digraph Set of vertices connected pairwise by directed

More information

CS 3410 Ch 14 Graphs and Paths

CS 3410 Ch 14 Graphs and Paths CS 3410 Ch 14 Graphs and Paths Sections 14.1-14.3 Pages 527-552 Exercises 1,2,5,7-9 14.1 Definitions 1. A vertex (node) and an edge are the basic building blocks of a graph. Two vertices, (, ) may be connected

More information

More Graph Algorithms: Topological Sort and Shortest Distance

More Graph Algorithms: Topological Sort and Shortest Distance More Graph Algorithms: Topological Sort and Shortest Distance Topological Sort The goal of a topological sort is given a list of items with dependencies, (ie. item 5 must be completed before item 3, etc.)

More information

CS4800: Algorithms & Data Jonathan Ullman

CS4800: Algorithms & Data Jonathan Ullman CS4800: Algorithms & Data Jonathan Ullman Lecture 11: Graphs Graph Traversals: BFS Feb 16, 2018 What s Next What s Next Graph Algorithms: Graphs: Key Definitions, Properties, Representations Exploring

More information

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V) Graph Algorithms Graphs Graph G = (V, E) V = set of vertices E = set of edges (V V) Types of graphs Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self loops.) Directed: (u, v) is edge from

More information

CS Algorithms and Complexity

CS Algorithms and Complexity CS 350 - Algorithms and Complexity Graph Theory, Midterm Review Sean Anderson 2/6/18 Portland State University Table of contents 1. Graph Theory 2. Graph Problems 3. Uninformed Exhaustive Search 4. Informed

More information

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington Graphs CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington 1 Representation Adjacency matrix??adjacency lists?? Review Graphs (from CSE 2315)

More information

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico CS 361 Data Structures & Algs Lecture 15 Prof. Tom Hayes University of New Mexico 10-12-2010 1 Last Time Identifying BFS vs. DFS trees Can they be the same? Problems 3.6, 3.9, 3.2 details left as homework.

More information

CS171 Final Practice Exam

CS171 Final Practice Exam CS171 Final Practice Exam Name: You are to honor the Emory Honor Code. This is a closed-book and closed-notes exam. You have 150 minutes to complete this exam. Read each problem carefully, and review your

More information

Chapter 4. Uninformed Search Strategies

Chapter 4. Uninformed Search Strategies Chapter 4. Uninformed Search Strategies An uninformed (a.k.a. blind, brute-force) search algorithm generates the search tree without using any domain specific knowledge. The two basic approaches differ

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define

More information

Unweighted Graphs & Algorithms

Unweighted Graphs & Algorithms Unweighted Graphs & Algorithms Zachary Friggstad Programming Club Meeting References Chapter 4: Graph (Section 4.2) Chapter 22: Elementary Graph Algorithms Graphs Features: vertices/nodes/dots and edges/links/lines

More information

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY General definitions; Representations; Graph Traversals; Topological sort; Graphs definitions & representations Graph theory is a fundamental tool in sparse

More information

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02 CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02 Topics: Graph Algorithms 1 Graph Algorithms There are many algorithms that can be applied to graphs. Many of these are actually used in the

More information

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305 Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n

More information

UNIT 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 information

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 1 Depth first search 1.1 The Algorithm Besides breadth first search, which we saw in class in relation to Dijkstra s algorithm, there is one

More information

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Lecture 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 information