Graphs CSE 2011 Winter 2011 28 November 2018.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

Graph & BFS.
CSC 213 – Large Scale Programming Lecture 27: Graph ADT.
CSC 213 ORD DFW SFO LAX Lecture 20: Graphs.
© 2004 Goodrich, Tamassia Graphs1 ORD DFW SFO LAX
B.Ramamurthy1 Graphs Chapter 12 B.Ramamurthy. 2 Introduction A structure that represents connectivity information. A tree is kind of graph. Applications.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT TTIT33 Algorithms and optimization Algorithms Lecture 4 Graphs.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
Graphs1 Part-H1 Graphs ORD DFW SFO LAX
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
CSC311: Data Structures 1 Chapter 13: Graphs I Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
ECE669 L10: Graph Applications March 2, 2004 ECE 669 Parallel Computer Architecture Lecture 10 Graph Applications.
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Lecture17: Graph I Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Graphs – ADTs and Implementations ORD DFW SFO LAX
Graphs Chapter 12.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is NOT meant by term “Graph”  Why no bar charts, scatter plots, & pie charts mentioned.
Graphs Part 1. Outline and Reading Graphs (§13.1) – Definition – Applications – Terminology – Properties – ADT Data structures for graphs (§13.2) – Edge.
Graphs1 Definitions Examples The Graph ADT LAX PVD LAX DFW FTL STL HNL.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
CSC 213 – Large Scale Programming. Today’s Goals  Briefly review graphs and vital graph terms  Begin discussion of how to implement Graph  Vertex &
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
Spring 2007Graphs1 ORD DFW SFO LAX
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
October 21, Graphs1 Graphs CS 221 (slides from textbook author)
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
CSC 213 – Large Scale Programming. Graphs  Mathematically, graph is pair (V, E) where  V is collection of nodes, called vertices  Two nodes can be.
Graphs Quebec Toronto Montreal Ottawa 449 km 255 km 200 km 545 km Winnipeg 2075 km 2048 km New York 596 km 790 km 709 km.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University Graphs Original Slides by Rada Mihalcea.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 COMP9024: Data Structures and Algorithms Week Eleven: Graphs (I) Hui Wu Session 1, 2016
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
14 Graph Algorithms Hongfei Yan June 8, 2016.
Graphs.
Graphs 5/14/ :46 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs 7/18/2018 7:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graph ADT and Algorithmic Paradigms
Graphs ORD SFO LAX DFW Graphs Graphs
COMP9024: Data Structures and Algorithms
Graphs ORD SFO LAX DFW Graphs Graphs
COMP9024: Data Structures and Algorithms
Graphs Part 1.
CMSC 341 Lecture 21 Graphs (Introduction)
CSE 373: Data Structures and Algorithms
Discrete Maths 9. Graphs Objective
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs.
Graphs.
Graph & BFS.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs 4/29/15 01:28:20 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Graphs Part 1 ORD SFO LAX DFW
Algorithms: Design and Analysis
Graphs By Rajanikanth B.
Graphs ORD SFO LAX DFW /15/ :57 AM
Graphs.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9
Presentation transcript:

Graphs CSE 2011 Winter 2011 28 November 2018

Graphs PVD ORD SFO LGA HNL LAX DFW MIA A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs of vertices, called edges Vertices and edges are objects and store elements Example: A vertex represents an airport and stores the three-letter airport code An edge represents a flight route between two airports and stores the mileage of the route 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA

Edge Types flight AA 1206 ORD PVD 849 miles ORD PVD Directed edge ordered pair of vertices (u,v) first vertex u is the origin second vertex v is the destination e.g., a flight Undirected edge unordered pair of vertices (u,v) e.g., a flight route Directed graph (digraph) all the edges are directed e.g., flight network Undirected graph all the edges are undirected e.g., route network Mixed graph: contains both directed and undirected edges flight AA 1206 ORD PVD 849 miles ORD PVD

Applications Electronic circuits Transportation networks Printed circuit board Integrated circuit Transportation networks Highway network Flight network Computer networks Local area network Internet Web Databases Entity-relationship diagram

Terminology End vertices (or endpoints) of an edge U and V are the endpoints of a Edges incident on a vertex a, d, and b are incident on V Adjacent vertices U and V are adjacent Degree of a vertex W has degree 4 Loop j is a loop (we will consider only loopless graphs) X U V W Z Y a c b e d f g h i j

Terminology (2) For directed graphs: Origin, destination of an edge Outgoing edge Incoming edge Out-degree of vertex v: number of outgoing edges of v In-degree of vertex v: number of incoming edges of v A B C D

Paths V a b P1 d U X Z P2 h c e W g f Y Path Path length Simple path sequence of alternating vertices and edges begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints Path length the total number of edges on the path Simple path path such that all vertices are distinct (except that the first and last could be the same) Examples P1=(V,b,X,h,Z) is a simple path P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple V a b P1 d U X Z P2 h c e W g f Y

Properties of Undirected Graphs Property 1 Sv deg(v) = 2E Proof: each edge is counted twice Property 2 In an undirected graph with no loops E  V (V - 1)/2 Proof: each vertex has degree at most (V - 1) What is the bound for a directed graph? Notation V number of vertices E number of edges deg(v) degree of vertex v Example V = 4 E = 6 deg(v) = 3

Cycles Cycle Simple cycle Examples circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints Simple cycle cycle such that all its vertices are distinct (except the first and the last) Examples C1=(V,b,X,g,Y,f,W,c,U,a,V) is a simple cycle C2=(U,c,W,e,X,g,Y,f,W,d,V,a,U) is a cycle that is not simple A directed graph is acyclic if it has no cycles  called DAG (directed acyclic graph) V a b d U X Z C2 h e C1 c W g f Y

Connectivity  Undirected Graphs connected not connected An undirected graph is connected if there is a path from every vertex to every other vertex.

Connectivity  Directed Graphs A directed graph is called strongly connected if there is a path from every vertex to every other vertex. If a directed graph is not strongly connected, but the corresponding undirected graph is connected, then the directed graph is said to be weakly connected.

Graph ADT and Data Structures CSE 2011

Representation of Graphs Two popular computer representations of a graph: Both represent the vertex set and the edge set, but in different ways. Adjacency Matrices Use a 2D matrix to represent the graph Adjacency Lists Use a set of linked lists, one list per vertex

Adjacency Matrix Representation 2D array of size n x n where n is the number of vertices in the graph A[i][j]=1 if there is an edge connecting vertices i and j; otherwise, A[i][j]=0

Adjacency Matrix Example 1 2 3 4 5 6 7 8 9 2 4 3 5 1 7 6 9 8

Adjacency Matrices: Analysis The storage requirement is (V2). not efficient if the graph has few edges. appropriate if the graph is dense; that is E= (V2) If the graph is undirected, the matrix is symmetric. There exist methods to store a symmetric matrix using only half of the space. Note: the space requirement is still (V2). We can detect in O(1) time whether two vertices are connected.

Adjacency Lists If the graph is sparse, a better solution is an adjacency list representation. For each vertex v in the graph, we keep a list of vertices adjacent to v.

Adjacency List Example 1 2 3 4 5 6 7 8 9 8 2 4 3 5 1 7 6 9 8 2 3 7 9 1 4 8 1 4 5 2 3 3 6 5 7 1 6 2 9 1 8

Adjacency Lists: Analysis Space =  (V + v deg(v)) =  (V + E) Testing whether u is adjacency to v takes time O(deg(v)) or O(deg(u)).

Adjacency Lists vs. Adjacency Matrices An adjacency list takes (V + E). If E = O(V2) (dense graph), both use (V2) space. If E = O(V) (sparse graph), adjacency lists are more space efficient. Adjacency lists More compact than adjacency matrices if graph has few edges Requires more time to find if an edge exists Adjacency matrices Always require (V2) space This can waste lots of space if the number of edges is small Can quickly find if an edge exists

(Undirected) Graph ADT Vertices and edges are positions store elements Define Vertex and Edge interfaces, each extending Position interface Accessor methods endVertices(e): returns an array of the two end vertices of e opposite(v, e): returns the vertex opposite of v on e areAdjacent(v, w): returns true iff v and w are adjacent replace(v, x): returns replace element at vertex v with x replace(e, x): returns replace element at edge e with x

(Undirected) Graph ADT (2) Update methods insertVertex(o): inserts a vertex storing element o insertEdge(v, w, o): inserts an edge (v, w) storing element o removeVertex(v): removes vertex v (and its incident edges) removeEdge(e): removes edge e Iterator methods incidentEdges(v): returns the edges incident to v vertices(): returns all vertices in the graph edges(): returns all edges in the graph

Homework Prove the big-Oh running time of the graph methods shown in the next slide.

Running Time of Graph Methods n vertices, m edges no parallel edges no self-loops bounds are “big-Oh” Edge List Adjacency List Adjacency Matrix Space n + m n2 incidentEdges(v) m deg(v) n areAdjacent (v, w) min(deg(v), deg(w)) 1 insertVertex(o) insertEdge(v, w, o) removeVertex(v) removeEdge(e)

Next Lectures Graph traversal Review Final exam Breadth first search (BFS) Applications of BFS Depth first search (DFS) Review Final exam