Graphs.

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms Lecture 20: Graphs II.
Advertisements

CS16: Introduction to Data Structures & Algorithms
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
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.
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.
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.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Graph A graph G is a set V(G) of vertices (nodes) and a set E(G) of edges which are pairs of vertices. abcd e i fgh jkl V = { a, b, c, d, e, f, g, h, i,
October 21, Graphs1 Graphs CS 221 (slides from textbook author)
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
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 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.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs CSE 2011 Winter November 2018.
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),
Graph Operations And Representation
Graphs ORD SFO LAX DFW 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.
CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9
GRAPHS.
Presentation transcript:

Graphs

Graphs 1843 802 1743 337 1233 ORD SFO LAX DFW Graphs Graphs 1/11/2018 1:07 PM Graphs 1843 ORD SFO 802 1743 337 LAX 1233 DFW Graphs

Graphs A graph is a pair (V, E), where Example: 849 1843 142 802 1743 V is a set of nodes, called vertices E is a collection of pairs of vertices, called edges Vertices and edges are positions 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 Graphs

Can you think of other examples of graphs modelling real-world situations? What are the nodes. (what data is stored) What are the edges. (what data is stored) Is it directed or undirected?

2 Edge Types flight AA 1206 849 miles Directed edge (ARROW) ordered pair of vertices (u,v) first vertex u is the origin second vertex v is the destination e.g., a flight Undirected edge (no arrow) unordered pair of vertices (u,v) e.g., a flight route Directed graph all the edges are directed e.g., route network Undirected graph all the edges are undirected e.g., flight network flight AA 1206 ORD PVD 849 miles ORD PVD A road between two points Could be one way (directed) Or two way (undirected) Could have more than one edge (e.g. toll road and public road) Graphs

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 Graphs

Terminology a c b e d f g h i j 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 X has degree 5 Parallel edges h and i are parallel edges Self-loop j is a self-loop X U V W Z Y a c b e d f g h i j Graphs

Terminology (cont.) a b P1 d P2 h c e g f Path Simple path Examples sequence of alternating vertices and edges begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints Simple path path such that all its vertices and edges are distinct 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 Graphs

Terminology (cont.) a b d C2 h e C1 c g f 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 and edges are distinct Examples C1=(V,b,X,g,Y,f,W,c,U,a,) is a simple cycle C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y Graphs

What operations do you think we can do to a graph add an edge add a node Others……

Main Methods of the Graph ADT Update (set or mutator) methods insertVertex(o): insert a vertex storing element o insertEdge(v, w, o): insert an edge (v,w) storing element o removeVertex(v): remove vertex v (and its incident edges) removeEdge(e): remove edge e collection methods incidentEdges(v): edges incident to v vertices(): all vertices in the graph edges(): all edges in the graph Vertices and edges are positions store elements Accessor (get) methods endVertices(e): an array of the two endvertices of e opposite(v, e): the vertex opposite of v on e areAdjacent(v, w): true iff v and w are adjacent replace(v, x): replace element at vertex v with x replace(e, x): replace element at edge e with x Graphs

Properties Sv deg(v) = 2m Property 1 (handshakes) Notation Example Proof: each edge is counted twice Notation n number of vertices m number of edges deg(v) degree of vertex v Example n = 4 m = 6 deg(v) = 3 Graphs

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

Representation There are different ways to represent a graph List of edges For each node – a list of adjacent nodes Adjacency matrix.

Performance Edge List Adjacency List Adjacency Matrix Space n + m n2 n vertices, m edges no parallel edges no self-loops 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) Graphs

Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph Depth-First Search

Connectivity A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components Depth-First Search

Trees and Forests A (free) tree is an undirected graph T such that T is connected T has no cycles This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Tree Forest Depth-First Search

Implementing a Graph To program a graph data structure, what information would we need to store? For each vertex? For each edge? 1 2 3 4 5 6 7

Implementing a Graph What kinds of questions would we want to be able to answer (quickly?) about a graph G? Where is vertex v? Which vertices are adjacent to vertex v? What edges touch vertex v? What are the edges of G? What are the vertices of G? What is the degree of vertex v? 1 2 3 4 5 6 7

Graph Implementation Strategies Edge List Adjacency Matrix Adjacency List

Edge List edge list: an unordered list of all edges in the graph 2 1 3 4 5 6 7 1 2 5 6 7 3 4

Edge List: Pros and Cons advantages easy to loop/iterate over all edges disadvantages hard to tell if an edge exists from A to B hard to tell how many edges a vertex touches (its degree) 1 2 5 6 7 3 4

Adjacency Matrix adjacency matrix: an n × n matrix where: the nondiagonal entry aij is the number of edges joining vertex i and vertex j (or the weight of the edge joining vertex i and vertex j) the diagonal entry aii corresponds to the number of loops (self-connecting edges) at vertex i

Adjacency Matrix: Pros and Cons advantages fast to tell whether edge exists between any two vertices i and j (and to get its weight) disadvantages consumes a lot of memory on sparse graphs (ones with few edges) redundant information for undirected graphs

Adjacency Matrix Example How do we figure out the degree of a given vertex? How do we find out whether an edge exists from A to B? How could we look for loops in the graph? 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7

Adjacency Lists adjacency list: stores edges as individual linked lists of references to each vertex's neighbors

Adjacency List: Pros and Cons advantages: new nodes can be added easily new nodes can be connected with existing nodes easily "who are my neighbors" easily answered disadvantages: determining whether an edge exists between two nodes: O(average degree)

Adjacency List Example How do we figure out the degree of a given vertex? How do we find out whether an edge exists from A to B? How could we look for loops in the graph? 1 2 3 4 5 6 7 1 2 3 4 5 6 7

Runtime table n vertices, m edges no parallel edges no self-loops Edge List Adjacency List Adjacency Matrix Space n + m n2 Finding all adjacent vertices to v m deg(v) n Determining if v is adjacent to w 1 adding a vertex adding an edge to v removing vertex v n? removing an edge from v n vertices, m edges no parallel edges no self-loops Edge List Adjacency List Adjacency Matrix Space Finding all adjacent vertices to v Determining if v is adjacent to w adding a vertex adding an edge removing vertex v removing an edge