Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Slides:



Advertisements
Similar presentations
Review Binary Search Trees Operations on Binary Search Tree
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 4.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs. Data structures that connect a set of objects to form a kind of a network Objects are called “Nodes” or “Vertices” Connections are called “Edges”
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS171 Introduction to Computer Science II Graphs Strike Back.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
CSCI 256 Data Structures and Algorithm Analysis Lecture 4 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
3.1 Basic Definitions and Applications. Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
3.1 Basic Definitions and Applications
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 6.
1 Chapter 3 Graphs Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Graphs Upon completion you will be able to:
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Data Structures and Algorithm Analysis Lecture 5
Graphs Chapter 20.
3.1 Basic Definitions and Applications
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithm Course Dr. Aref Rashad
Lecture 10 Graph Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

These are not Graphs...not the kind we mean, anyway

These are Graphs K5K5 K 3,3 =

Applications of Graphs  Communication networks  Routing and shortest path problems  Commodity distribution (flow)  Traffic control  Resource allocation  Geometric modeling ...

Undirected Graphs Undirected graph. G = (V, E) is an ordered pair consisting of n V = nodes. n E = edges between pairs of nodes. n Captures pairwise relationship between objects. n Graph size parameters: n = |V|, m = |E|. n Two nodes connected by an edge are said to be neighbor. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Some Properties of Undirected Graph n |E|= O(V 2 ) n If G is connected |E|≥|V|–1 – An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v. n deg(V) is defined as the number of edges incident to V – Handshaking lemma: Σ v ∈ V deg(v) = 2|E|

Directed Graphs Directed graph. G = (V, E) n Edge (u, v) goes from node u to node v. Ex. Web graph - hyperlink points from one web page to another. n Directedness of graph is crucial. n Modern web search engines exploit hyperlink structure to rank web pages by importance.

World Wide Web Web graph. n Node: web page. n Edge: hyperlink from one page to another. cnn.com cnnsi.com novell.comnetscape.com timewarner.com hbo.com sorpranos.com

Graph Representation: Adjacency Matrix Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. n Two representations of each edge. n Space proportional to n 2. n Checking if (u, v) is an edge takes O(1) time. n Identifying all edges takes O(n 2 ) time. n Use for dense graph

Graph Representation: Adjacency List Adjacency list. Node indexed array of lists. n Two representations of each edge. n Space proportional to m + n. n Checking if (u, v) is an edge takes O(deg(u)) time. n Identifying all edges takes O(m + n) time. n Use for sparse graph degree = number of neighbors of u 37

Paths and Connectivity Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1, v 2, …, v k-1, v k with the property that each consecutive pair v i, v i+1 is joined by an edge in E. Def. A path is simple if all nodes are distinct. Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v.

Cycles Def. A cycle is a path v 1, v 2, …, v k-1, v k in which v 1 = v k, k > 2, and the first k-1 nodes are all distinct. cycle C =

Trees Def. An undirected graph is a tree if it is connected and does not contain a cycle. Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third. n G is connected. n G does not contain a cycle. n G has n-1 edges.

Rooted Trees Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. Importance. Models hierarchical structure. a tree the same tree, rooted at 1 v parent of v child of v root r

Graph Traversal n Problem: Search for a certain node or traverse all nodes in the graph n Depth First Search – Once a possible path is found, continue the search until the end of the path n Breadth First Search – Start several paths at a time, and advance in each one step at a time

Depth First Traversal n A natural way to do Depth-first search (BFS) is using recursion. DFS( Node c ) { Mark c "Visited” For each neighbor n of c If n "Unvisited" DFS( n ) } Possible visit sequence: 1, 2, 4, 5, 6, 3, 8, 7

Breadth First Traversal n Breadth-first search (BFS) not naturally recursive. n Use a queue so that vertices are visited in order according to their distance from the starting vertex. BFS(Node v) { create a queue Q enqueue v onto Q mark v “Visited” while Q is not empty { dequeue t from Q for each neighbor u of t do if u is not marked { mark u “Visited” enqueue u onto Q }

Breadth First Search L0L0 L1L1 L2L2 L3L3 Visit sequence: 1, 2, 3, 4, 5, 7, 8, 6

Applications: Finding a Path n Find path from source vertex s to destination vertex d n Use graph search starting at s and terminating as soon as we reach d n Need to remember edges traversed n Use depth – first search

DFS E F G B C D A start destination A DFS on A A DFS on B B A DFS on C B C A B Return to call on B D Call DFS on D A B D Call DFS on G G found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G DFS Process

Minimum spanning trees Minimum spanning tree. Given a connected undirected graph G = (V, E) with real-valued edge weights c e, an MST is a subset of the edges T  E such that T is a spanning tree whose sum of edge weights is minimized.

Minimum spanning trees

Greedy Algorithms Prim's algorithm. Start with some root node s and greedily grow a tree T from s outward. At each step, add the cheapest edge e to T that has exactly one endpoint in T. Theorem. Let T be the MST of G= (V, E), and let A ⊆ V. Suppose that (u, v) ∈ E is the least-weight edge connecting A to V–A. Then, (u, v) ∈ T.

Greedy Algorithms Proof: Suppose (u, v) ∉ T. Consider the unique simple path from u to v in T. Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V–A to get a lower-weight MST