Graphs. Data Structures 1 Graphs

Size: px
Start display at page:

Download "Graphs. Data Structures 1 Graphs"

Transcription

1 Graphs Graph Applications Definitions Graph Representations(adjacency matrix/list) Graph ADT Graph Traversals: BFS, DFS Shortest Path Algorithms Minimum Spanning Tree Algorithms Data Structures Graphs

2 Graph Applications Communication Networks (Internet) Social networks. Geographic Maps (locations + distances) Transportation Networks (flow capacity) Scientific, familial, business taxonomies Data Structures 2 Graphs

3 Definitions and Terminology Graph: G = (V, E), set of vertices V, and a set of edges E connecting pairs of vertices. Sparse, Dense graphs. Complete Graph: Includes all possible edges. Digraph: Directed Graph, where edges are directed. Undirected Graph: Edges are undirected. Data Structures 3 Graphs

4 Definitions (Contd) Adjacency, Neighbors: vertices sharing an edge Incidence: Edge (u, v) is incident on verices u and v. Weighted Edges: Edges have weight. Path: Sequence of vertices v, v 2,.., v n connected by edges, of path length n. Simple Path: All vertices are distinct. Cycle: Path of length 3 or more connecting some vertex to itself. Acyclic: Graph with no cycles. Data Structures Graphs

5 Definitions (Contd) DAG: Directed Acyclic Graph. Simple Cycle: Path is simple. Subgraph: Select a subset V s of G s vertices and some edges of G. Connected: Graph that has at least one path from any vertex to any other. Connected Components: Maximally connected subgraphs of an undirected graph. Data Structures 5 Graphs

6 Graph: Connected Components An example graph with 3 connected components. Data Structures 6 Graphs

7 Graph Representations Two commonly used representations: Adjacency Matrix, Adjacency List Adjacency Matrix An V V 2D array (matrix), the dimensions representing a vertex v i v 0, v n Element (i, j) is a bit, which is, if an edge exists between v i and v j, else it is 0. Requires Θ(n 2 ) space. Data Structures 7 Graphs

8 Adjacency Matrix Representation Data Structures 8 Graphs

9 Graph Representation:Adjacency List An array of size V, each of which is a linked list. Position i of the array, stores all edges from vertex v i ; each node contains the terminating vertex, v j. Requires Θ( V + E ) storage. Data Structures 9 Graphs

10 Adjacency List Representation Data Structures 0 Graphs

11 Graph Implementation: ADT public interface Graph { / / Graph class ADT / I n i t i a l i z e the n The number of v e r t i c e s / public void I n i t ( i n t n ) ; The number of v e r t i c e s / public i n t n ( ) ; The c u r r e n t number of edges / public i n t e ( ) ; v s f i r s t neighbor / public i n t f i r s t ( i n t v ) ; v s next neighbor / public i n t next ( i n t v, i n t w ) ; / Set the weight f o r an i, j The v e r t i c e wght Edge weight / public void setedge ( i n t i, i n t j, i n t wght ) ; Data Structures Graphs

12 Graph Implementation: ADT / Delete an i, j The v e r t i c e s / public void deledge ( i n t i, i n t j ) ; / Determine i f an edge i s i n the i, j The v e r t i c e t r u e i f edge i, j has non zero weight / public boolean isedge ( i n t i, i n t j ) ; / Return an edge s i, j The v e r t i c e The weight of edge i, j, or zero / public i n t weight ( i n t i, i n t j ) ; / Set the mark value f o r a v The v a l The value to set / public void setmark ( i n t v, i n t v a l ) ; / Get the mark value f o r a v The The value of the mark / public i n t getmark ( i n t v ) ; Data Structures 2 Graphs

13 Graph Implementation Vertices are indices, edges are vertex pairs. Navigation: first(), next(). Edge functions: isedge, setedge(), deledge(). Weight(), Mark() functions Data Structures 3 Graphs

14 Graph Implementation: Adjacency Matrix first(int v), next(int v, int w): Must search a matrix row for the appropriate edge. setedge(int v, int w, Weight w), weight) : Set/Get the weight for a particular edge in the matrix. IsEdge(int v, int w): Tests if this edge exists. deledge(int v, int w): Deletes edge, if it exists. getmark(int v), setmark(int v, int val) : a mark array - useful in traversal algorithms Data Structures Graphs

15 Graph Implementation: Adjacency Matrix class Graphm implements Graph { / / Graph : Adjacency m a t r i x private i n t [ ] [ ] matrix ; / / The edge matrix private i n t numedge ; / / Number of edges public i n t [ ] Mark ; / / The mark array public Graphm ( ) { public Graphm ( i n t n ) { / / Constructor I n i t ( n ) ; public void I n i t ( i n t n ) { Mark = new i n t [ n ] ; m a t r i x = new i n t [ n ] [ n ] ; numedge = 0; public i n t n ( ) { return Mark. length ; / / # of v e r t i c e s public i n t e ( ) { return numedge ; / / # of edges public i n t f i r s t ( i n t v ) { / / Get v s f i r s t neighbor for ( i n t i =0; i <Mark. length ; i ++) i f ( m a trix [ v ] [ i ]!= 0) return i ; return Mark. length ; / / No edge f o r t h i s vertex Data Structures 5 Graphs

16 Graph Implementation: Adjacency Matrix public i n t next ( i n t v, i n t w) { / / Get v s next edge for ( i n t i =w+; i <Mark. length ; i ++) i f ( m a trix [ v ] [ i ]!= 0) return i ; return Mark. length ; / / No next edge ; / / Set edge weight public void setedge ( i n t i, i n t j, i n t wt ) { a s s e r t wt!=0 : Cannot set weight to 0 ; i f ( m a t r ix [ i ] [ j ] == 0) numedge++; m a t r i x [ i ] [ j ] = wt ; public void deledge ( i n t i, i n t j ) { / / Delete edge ( i, j ) i f ( m a t r ix [ i ] [ j ]!= 0) numedge ; m a t r i x [ i ] [ j ] = 0; public boolean isedge ( i n t i, i n t j ) / / I s ( i, j ) an edge? { return matrix [ i ] [ j ]!= 0; public i n t weight ( i n t i, i n t j ) { / / Return edge weight return matrix [ i ] [ j ] ; / / Get and set marks public void setmark ( i n t v, i n t v a l ) { Mark [ v ] = v a l ; public i n t getmark ( i n t v ) { return Mark [ v ] ; Data Structures 6 Graphs

17 Graph Implementation: Adjacency List Use an extension of the List class; allocate the required number of lists as an array of lists. first(int v), next(int v, int w): first or next element corresponding to index v ; setedge(int i, int j, int w) : Set the weight for an existing edge, or insert a new edge. IsEdge(int v, int w): Tests if this edge exists. deledge(int v, int w): Deletes edge, if it exists. setmark(int v, int val) : a Mark array is marked - useful in traversal algorithms int getmark(int v): returns mark value of v. Data Structures 7 Graphs

18 Graph Implementation: Adjacency List class Graphl implements Graph { private GraphList [ ] vertex ; / / The vertex l i s t private i n t numedge ; / / Number of edges public i n t [ ] Mark ; / / The mark array public Graphl ( ) { public Graphl ( i n t n ) / / Constructor { I n i t ( n ) ; public void I n i t ( i n t n ) { Mark = new i n t [ n ] ; v e r t e x = new GraphList [ n ] ; for ( i n t i =0; i <n ; i ++) v e r t e x [ i ] = new GraphList ( ) ; numedge = 0; public i n t n ( ) { return Mark. length ; / / # of v e r t i c e s public i n t e ( ) { return numedge ; / / # of edges Data Structures 8 Graphs

19 Graph Implementation: Adjacency List public i n t f i r s t ( i n t v ) { / / Get v s f i r s t neighbor i f ( v e r t ex [ v ]. length ( ) == 0) return Mark. length ; / / No neighbor v e r t e x [ v ]. movetostart ( ) ; Edge i t = v ertex [ v ]. getvalue ( ) ; return i t. vertex ( ) ; public i n t next ( i n t v, i n t w) { / / Get next neighbor Edge i t = null ; i f ( isedge ( v, w) ) { v e r t e x [ v ]. next ( ) ; i t = vertex [ v ]. getvalue ( ) ; i f ( i t!= null ) return i t. vertex ( ) ; else return Mark. length ; Data Structures 9 Graphs

20 Graph Implementation: Adjacency List / / Store edge weight public void setedge ( i n t i, i n t j, i n t weight ) { a s s e r t weight!= 0 : May not set weight to 0 ; Edge curredge = new Edge ( j, weight ) ; i f ( isedge ( i, j ) ) { / / Edge already e x i s t s i n graph v e r t e x [ i ]. remove ( ) ; v e r t e x [ i ]. i n s e r t ( curredge ) ; else { / / Keep neighbors sorted by vertex index numedge++; for ( vertex [ i ]. movetostart ( ) ; vertex [ i ]. currpos ( ) < vertex [ i ]. length ( ) ; vertex [ i ]. next ( ) ) i f ( vertex [ i ]. getvalue ( ). vertex ( ) > j ) break ; v e r t e x [ i ]. i n s e r t ( curredge ) ; public void deledge ( i n t i, i n t j ) / / Delete edge { i f ( isedge ( i, j ) ) { vertex [ i ]. remove ( ) ; numedge ; Data Structures 20 Graphs

21 Graph Implementation: Adjacency List public boolean isedge ( i n t v, i n t w) { / / I s ( i, j ) an edge? Edge i t = v ertex [ v ]. getvalue ( ) ; i f ( ( i t!= null ) && ( i t. vertex ( ) == w) ) return true ; for ( v e r tex [ v ]. movetostart ( ) ; v e rtex [ v ]. currpos ( ) < vertex [ v ]. length ( ) ; v e rtex [ v ]. next ( ) ) / / Check whole l i s t i f ( v ertex [ v ]. getvalue ( ). vertex ( ) == w) return true ; return false ; public i n t weight ( i n t i, i n t j ) { / / Return weight of edge i f ( isedge ( i, j ) ) return vertex [ i ]. getvalue ( ). weight ( ) ; return 0; / / Set and get marks public void setmark ( i n t v, i n t v a l ) { Mark [ v ] = v a l ; public i n t getmark ( i n t v ) { return Mark [ v ] ; Data Structures 2 Graphs

22 Graph Traversals Goal is to visit the nodes of a graph. Need a start vertex. Problems: graph may be disconnected, and contain cycles. Use a mark bit to identify visited nodes. void graphtraverse ( Graph G) { for ( v =0; v<g. n ( ) ; v ++) G. setmark ( v, UNVISITED ) ; / / I n i t i a l i z e mark b i t s for ( v =0; v<g. n ( ) ; v ++) i f (G. getmark ( v ) == UNVISITED ) dotraverse (G, v ) ; Data Structures 22 Graphs

23 Depth First Search (DFS) When a node v is visited, recursively visit all of its neighbors. Effect is to follow one branch all the way through and then back up and follow another branch, etc. A A D D C C B E B E F F Data Structures 23 Graphs

24 DFS Implementation s t a t i c void DFS( Graph G, i n t v ) { / / Depth f i r s t search P r e V i s i t (G, v ) ; / / Take a p p r o p r i a t e a c t i o n G. setmark ( v, VISITED ) ; for ( i n t w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next ( v, w) ) i f (G. getmark (w) == UNVISITED ) DFS(G, w ) ; P o s t V i s i t (G, v ) ; / / Take a p p r o p r i a t e a c t i o n Data Structures 2 Graphs

25 Breadth First Search(BFS) Examine all vertices connected to a vertex v, before visiting vertices deeper (further away). Equivalent to level traversal of a tree. A A D D C C B E B E F F Data Structures 25 Graphs

26 BFS Implementation / / Breadth f i r s t ( queue based ) search s t a t i c void BFS( Graph G, i n t s t a r t ) { Queue<Integer> Q = new AQueue<Integer >(G. n ( ) ) ; Q. enqueue ( s t a r t ) ; G. setmark ( s t a r t, VISITED ) ; while (Q. l e n g t h ( ) > 0) { / / Process each v e r t e x on Q i n t v = Q. dequeue ( ) ; P r e V i s i t (G, v ) ; / / Take a p p r o p r i a t e a c t i o n for ( i n t w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next ( v, w) ) i f (G. getmark (w) == UNVISITED ) / / Put neighbors on { G. setmark (w, VISITED ) ; Q. enqueue (w ) ; P o s t V i s i t (G, v ) ; / / Take a p p r o p r i a t e a c t i o n Data Structures 26 Graphs

27 Data Structures 27 Graphs

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University U Kang 1 In This Lecture Basic terms and definitions of graphs How to represent graphs Graph traversal methods U Kang 2 Graphs

More information

11.1 Terminology and Representations

11.1 Terminology and Representations Graphs Graphs provide the ultimate in data structure flexibility. Graphs can model both real-world systems and abstract problems, so they are used in hundreds of applications. Here is a small sampling

More information

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs ECE4050 Data Structures and Algorithms Lecture 8: Graphs 1 Graphs A graph G = (V, E) consists of a set of vertices V, and a set of edges E, such that each edge in E is a connection between a pair of vertices

More information

// Source code example for "A Practical Introduction to Data Structures and // Algorithm Analysis" by Clifford A. Shaffer, Prentice Hall, 1998.

// Source code example for A Practical Introduction to Data Structures and // Algorithm Analysis by Clifford A. Shaffer, Prentice Hall, 1998. // Source code example for "A Practical Introduction to Data Structures and // Algorithm Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. // Copyright 1998 by Clifford A. Shaffer // graph.java interface

More information

Graph Applications. Topological Sort Shortest Path Problems Spanning Trees. Data Structures 1 Graph Applications

Graph Applications. Topological Sort Shortest Path Problems Spanning Trees. Data Structures 1 Graph Applications Graph Applications Topological Sort Shortest Path Problems Spanning Trees Data Structures 1 Graph Applications Application: Topological Sort Given a set of jobs, courses, etc. with prerequisite constraints,

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

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

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

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

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

Graph Traversals BFS & DFS 1 CS S-16

Graph Traversals BFS & DFS 1 CS S-16 CS-8S- BFS & DFS -: Visit every vertex, in an order defined by the topololgy of the graph. Two major traversals: Depth First Search Breadth First Search -: Depth First Search Starting from a specific node

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

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

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

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

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

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

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 5 Decrease-and-Conquer Copyright 2007 Pearson Addison-Wesley. All rights reserved. Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance

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

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

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies Graph Chapter 9 Outline Introduction Definitions and Basic Terminologies Representations of Graphs Graph Traversals Breadth first traversal Depth first traversal Applications Single source shortest path

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

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3 Multidimensional Arrays & Graphs CMSC 420: Lecture 3 Mini-Review Abstract Data Types: List Stack Queue Deque Dictionary Set Implementations: Linked Lists Circularly linked lists Doubly linked lists XOR

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

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS5-5S-6 Graph Traversals BFS & DFS David Galles Department of Computer Science University of San Francisco 6-: Graph Traversals Visit every vertex, in an order defined by

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

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

Graph Algorithms. Definition

Graph Algorithms. Definition Graph Algorithms Many problems in CS can be modeled as graph problems. Algorithms for solving graph problems are fundamental to the field of algorithm design. Definition A graph G = (V, E) consists of

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

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

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v ) Mystery Algorithm! 0 2 ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v ) 3 1 4 7 6 5 mystery( v ) mark vertex v as visited PRINT v for each vertex w adjacent

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

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

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

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Next: Graph Algorithms Graphs Ch 22 Graph representations adjacency list adjacency matrix Minimum Spanning Trees Ch 23 Traversing graphs Breadth-First Search Depth-First Search 11/30/17 CSE 3101 1 Graphs

More information

Graph Traversals. Ric Glassey

Graph Traversals. Ric Glassey Graph Traversals Ric Glassey glassey@kth.se Overview Graph Traversals Aim: Develop alternative strategies to moving through a graph by visiting vertices and travelling along edges Maze example Depth-first

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

Undirected Graphs. Hwansoo Han

Undirected Graphs. Hwansoo Han Undirected Graphs Hwansoo Han Definitions Undirected graph (simply graph) G = (V, E) V : set of vertexes (vertices, nodes, points) E : set of edges (lines) An edge is an unordered pair Edge (v, w) = (w,

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

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

Announcements Problem Set 4 is out!

Announcements Problem Set 4 is out! CSC263 Week 8 Announcements Problem Set 4 is out! Due Tuesday (Nov 17) Other Announcements Drop date Nov 8 Final exam schedule is posted CSC263 exam Dec 11, 2-5pm This week s outline Graphs BFS Graph A

More information

Inf 2B: Graphs II - Applications of DFS

Inf 2B: Graphs II - Applications of DFS Inf 2B: Graphs II - Applications of DFS Kyriakos Kalorkoti School of Informatics University of Edinburgh Reminder: Recursive DFS Algorithm dfs(g) 1. Initialise Boolean array visited by setting all entries

More information

Introduction to Graphs. CS2110, Spring 2011 Cornell University

Introduction to Graphs. CS2110, Spring 2011 Cornell University Introduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a set of nodes connected by edges. Synonym Graph Hostile Slick Icy

More information

Chapter 9. Priority Queue

Chapter 9. Priority Queue Chapter 9 Priority Queues, Heaps, Graphs Spring 2015 1 Priority Queue Priority Queue An ADT in which only the item with the highest priority can be accessed 2Spring 2015 Priority Depends on the Application

More information

Graph Representation

Graph Representation Graph Representation Adjacency list representation of G = (V, E) An array of V lists, one for each vertex in V Each list Adj[u] contains all the vertices v such that there is an edge between u and v Adj[u]

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

To list all vertices connected to a vertex u in graph G we can use the following loop:

To list all vertices connected to a vertex u in graph G we can use the following loop: Using nextdjacent() To list all vertices connected to a vertex u in graph we can use the following loop: for (int v=.nextdjacent(u,-1); v>=0; v=.nextdjacent(u,v)) { System.out.print(v); djacency Lists

More information

Data Structures Brett Bernstein

Data Structures Brett Bernstein Data Structures Brett Bernstein Final Review 1. Consider a binary tree of height k. (a) What is the maximum number of nodes? (b) What is the maximum number of leaves? (c) What is the minimum number of

More information

Representations of Graphs

Representations of Graphs ELEMENTARY GRAPH ALGORITHMS -- CS-5321 Presentation -- I am Nishit Kapadia Representations of Graphs There are two standard ways: A collection of adjacency lists - they provide a compact way to represent

More information

Data Structures and Algorithms Key to Homework Assignment 8

Data Structures and Algorithms Key to Homework Assignment 8 Data Structures and Algorithms Key to Homework Assignment 8. Apply the strong components algorithm to the digraph that has the following adjacency matrix: 0 4 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

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

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

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

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

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

Elementary Graph Algorithms

Elementary Graph Algorithms Elementary 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)

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

Algorithms Sequential and Parallel: A Unified Approach; R. Miller and L. Boxer 3rd Graph Algorithms

Algorithms Sequential and Parallel: A Unified Approach; R. Miller and L. Boxer 3rd Graph Algorithms Algorithms Sequential and Parallel: A Unified Approach; R. Miller and L. Boxer rd Edition @ 0 www.thestudycampus.com Graph Algorithms Terminology Representations Fundamental Algorithms Computing the Transitive

More information

CS61BL. Lecture 5: Graphs Sorting

CS61BL. Lecture 5: Graphs Sorting CS61BL Lecture 5: Graphs Sorting Graphs Graphs Edge Vertex Graphs (Undirected) Graphs (Directed) Graphs (Multigraph) Graphs (Acyclic) Graphs (Cyclic) Graphs (Connected) Graphs (Disconnected) Graphs (Unweighted)

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

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch] Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1 Directed Graphs Let V be a finite set and E a binary relation on V, that is, E VxV. Then the pair G=(V,E) is called a directed graph.

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CSE 5311 Lecture 18 Graph Algorithm Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Graphs Graph G = (V,

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

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

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

國立清華大學電機工程學系. Outline

國立清華大學電機工程學系. Outline 國立清華大學電機工程學系 EE Data Structure Chapter Graph (Part I) Outline The Graph Abstract Data Type Introduction Definitions Graph Representations Elementary Graph Operations Minimum Cost Spanning Trees ch.- River

More information

1. Graph and Representation

1. Graph and Representation 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

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms s of s Computer Science & Engineering 423/823 Design and Analysis of Lecture 03 (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 29 s of s s are abstract data types that are applicable

More information

22.1 Representations of graphs

22.1 Representations of graphs 22.1 Representations of graphs There are two standard ways to represent a (directed or undirected) graph G = (V,E), where V is the set of vertices (or nodes) and E is the set of edges (or links). Adjacency

More information

Graphs. Graph Structures

Graphs. Graph Structures Graphs A graph G consists of a set V of vertices and a set E of pairs of distinct vertices from V. These pairs of vertices are called edges. If the pairs of vertices are unordered, G is an undirected graph.

More information

COT 6405 Introduction to Theory of Algorithms

COT 6405 Introduction to Theory of Algorithms COT 6405 Introduction to Theory of Algorithms Topic 14. Graph Algorithms 11/7/2016 1 Elementary Graph Algorithms How to represent a graph? Adjacency lists Adjacency matrix How to search a graph? Breadth-first

More information

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl Graph Theory ICT Theory Excerpt from various sources by Robert Pergl What can graphs model? Cost of wiring electronic components together. Shortest route between two cities. Finding the shortest distance

More information

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 Graphs A graph, G = (V, E), consists of two sets: V is a finite non-empty set of vertices. E is a set of pairs of vertices, called

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

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

Trees. Arash Rafiey. 20 October, 2015

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

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1 Undirected Graphs Terminology. Free Trees. Representations. Minimum Spanning Trees (algorithms: Prim, Kruskal). Graph Traversals (dfs, bfs). Articulation points & Biconnected Components. Graph Matching

More information

CSC263 Week 8. Larry Zhang.

CSC263 Week 8. Larry Zhang. CSC263 Week 8 Larry Zhang http://goo.gl/forms/s9yie3597b Announcements (strike related) Lectures go as normal Tutorial this week everyone go to BA32 (T8, F2, F2, F3) Problem sets / Assignments are submitted

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

Chapter 22 Elementary Graph Algorithms

Chapter 22 Elementary Graph Algorithms Chapter 22 Elementary Graph Algorithms Graph Representations Graph G = (V,E) Directed Undirected Adjacency Lists Adjacency Matrix Graph Representations Adjacency List: Undirected Memory: Adjacency: Graph

More information

Chapter 28 Graphs and Applications. Objectives

Chapter 28 Graphs and Applications. Objectives Chapter 28 Graphs and Applications CS2: Data Structures and Algorithms Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox, Wim Bohm, and Russ Wakefield 1 Objectives

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

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

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

(Re)Introduction to Graphs and Some Algorithms

(Re)Introduction to Graphs and Some Algorithms (Re)Introduction to Graphs and Some Algorithms Graph Terminology (I) A graph is defined by a set of vertices V and a set of edges E. The edge set must work over the defined vertices in the vertex set.

More information

Graphs Introduction and Depth first algorithm

Graphs Introduction and Depth first algorithm Graphs Introduction and Depth first algorithm Carol Zander Introduction to graphs Graphs are extremely common in computer science applications because graphs are common in the physical world. Everywhere

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 4-63 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmuedu wwwvernoneu Data Structures and Algorithms for Engineers 1 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

Algorithms: Lecture 10. Chalmers University of Technology

Algorithms: Lecture 10. Chalmers University of Technology Algorithms: Lecture 10 Chalmers University of Technology Today s Topics Basic Definitions Path, Cycle, Tree, Connectivity, etc. Graph Traversal Depth First Search Breadth First Search Testing Bipartatiness

More information

22.3 Depth First Search

22.3 Depth First Search 22.3 Depth First Search Depth-First Search(DFS) always visits a neighbour of the most recently visited vertex with an unvisited neighbour. This means the search moves forward when possible, and only backtracks

More information

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths Part VI Graph algorithms Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths 1 Chapter 22 Elementary Graph Algorithms Representations of graphs

More information

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018 CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018 Q1: Prove or disprove: You are given a connected undirected graph G = (V, E) with a weight function w defined over its

More information

Unit 2: Algorithmic Graph Theory

Unit 2: Algorithmic Graph Theory Unit 2: Algorithmic Graph Theory Course contents: Introduction to graph theory Basic graph algorithms Reading Chapter 3 Reference: Cormen, Leiserson, and Rivest, Introduction to Algorithms, 2 nd Ed., McGraw

More information

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Problem Score Maximum MC 34 (25/17) = 50 Total 100 Stony Brook University Midterm 2 CSE 373 Analysis of Algorithms November 22, 2016 Midterm Exam Name: ID #: Signature: Circle one: GRAD / UNDERGRAD INSTRUCTIONS: This is a closed book, closed mouth exam.

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

CS2 Algorithms and Data Structures Note 9

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

Graph Search Methods. Graph Search Methods

Graph Search Methods. Graph Search Methods Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. 0 Graph Search Methods A search method starts at a given vertex v and visits/labels/marks every vertex that is

More information

Breadth First Search. cse2011 section 13.3 of textbook

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

Inf 2B: Graphs, BFS, DFS

Inf 2B: Graphs, BFS, DFS Inf 2B: Graphs, BFS, DFS Kyriakos Kalorkoti School of Informatics University of Edinburgh Directed and Undirected Graphs I A graph is a mathematical structure consisting of a set of vertices and a set

More information