CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Advertisements

Review Binary Search Trees Operations on Binary Search Tree
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 27 Graphs and Applications.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
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.
CS 206 Introduction to Computer Science II 11 / 09 / 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.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Chapter 2 Graph Algorithms.
Graphs – ADTs and Implementations ORD DFW SFO LAX
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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,
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. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
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.
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.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 30 Graphs and Applications.
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.
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.
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 Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Graphs Upon completion you will be able to:
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.
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.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Csc 2720 Instructor: Zhuojun Duan
CS120 Graphs.
Graphs Chapter 13.
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.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
Heaps Chapter 6 Section 6.9.
Presentation transcript:

CISC 235: Topic 9 Introduction to Graphs

CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals

CISC 235 Topic 93 Graphs A graph G = (V, E) is composed of: V: set of Vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example: V = {a,b,c,d,e} E = {(a,b),(a,c),(a,d),(b,e),(c,d),(c,e),(d,e)} a b d c e

CISC 235 Topic 94 Example TOR OTT KIN ROC SYR COR MON 450 km 100 km 150 km 250 km 100 km 90 km 170 km 200 km

CISC 235 Topic 95 Terminology An undirected graph has undirected edges. Each edge is associated with an unordered pair. A directed graph, or digraph, has directed edges. Each edge is associated with an ordered pair. A weighted graph is one in which the edges are labeled with numeric values. TO Ott Kin MO TO Ott MO

CISC 235 Topic 96 Undirected Graphs Adjacent (Neighbors): Two vertices connected by an edge are adjacent. Incident: The edge that connects two vertices is incident on both of them. Degree of a Vertex v, deg(v): The number of edges incident on it (loop at vertex is counted twice)

CISC 235 Topic 97 Directed Graphs Edge (u,v): u is adjacent to v v is adjacent from u deg - (v) : The in-degree of v, the number of edges entering it deg + (v): The out-degree of v, the number of edges leaving it

CISC 235 Topic 98 Euler & the Bridges of Koenigsberg Can one walk across each bridge exactly once and return to the starting point?

CISC 235 Topic 99 Eulerian Tour What characteristics are required of an undirected graph for a Eulerian Tour to be possible?

CISC 235 Topic 910 Terminology A path is a sequence of vertices v 1, v 2, … v k such that v i and v i +1 are adjacent. A simple path is a path that contains no repeated vertices, except for perhaps the first and last vertices in the path. A cycle is a simple path, in which the last vertex is the same as the first vertex. a b d c e a b d c e a b d c e a b d c e abecdecdeb edcedce

CISC 235 Topic 911 Terminology A graph is connected if, for any two vertices, there is a path between them. A tree is a connected graph without cycles. A subgraph of a graph G is a graph H whose vertices and edges are subsets of the vertices and edges of G. a b d c e a b d c e a b c e graph G1graph G2 graph G3graph G4 a b d c e

CISC 235 Topic 912 Terminology A forest is a graph that is a collection of trees. More simply, it is a graph without cycles. a b d c e a b d c e a b c e a b d c e

CISC 235 Topic 913 Terminology A complete graph is an undirected graph with every pair of vertices adjacent. a b e c d a b c b c a d a b a

CISC 235 Topic 914 Undirected Graphs: Properties If |E| = |V | -1 and the graph is connected, the graph is a tree If |E| < |V | -1, the graph is not connected If |E| > |V | -1, the graph has at least one cycle a b d c e a b d c e a b d c e

CISC 235 Topic 915 Undirected Graphs: Properties Let n = |V | Let m = |E | Sparse Graphs : m is O( n ) Dense Graphs : m is O( n 2 ) Are complete graphs dense graphs? a b d c e a b e c d

CISC 235 Topic 916 Complete Graphs K2K3K4 K2K3K4 a b e c d b c a d a b c a bn = 2 m = 1 n = 3 m = 3 n = 4 m = 6 K5 K5 n = 5 m = 10 For K n, m = n(n-1)/2

CISC 235 Topic 917 Representations of Graphs Adjacency List and Adjacency Matrix Representations of an Undirected Graph

CISC 235 Topic 918 Representations of Graphs Adjacency List and Adjacency Matrix Representations of a Directed Graph

CISC 235 Topic 919 Graph Implementation Data Store two sets of info: vertices & edges Data can be associated with both vertices & edges A Few Typical Operations adjacentVertices( v ) – Return list of adjacent vertices areAdjacent( v, w ) – True if vertex v is adjacent to w insertVertex( o ) – Insert new isolated vertex storing o insertEdge( v, w, o ) – Insert edge from v to w, storing o at this edge removeVertex( v ) – Remove v and all incident edges removeEdge( v, w ) – Remove edge (v,w)

CISC 235 Topic 920 Graph Representations Space Analysis Adjacency List: Adjacency Matrix:

CISC 235 Topic 921 Graph Representations Time Analysis A Few Common Operations Adjacency ListAdjacency Matrix areAdjacent( v, w ) adjacentVertices( v ) removeEdge( v, w )

CISC 235 Topic 922 Traversals: Breadth-First Search & Depth-First Search TOR OTT KIN ROC SYR COR MON 450 km 100 km 150 km 250 km 100 km 90 km 170 km 200 km

CISC 235 Topic 923 Breadth-First Search bfs( vertex v ) Create a queue, Q, of vertices, initially empty Visit v and mark it as visited Enqueue ( v, Q ) while not empty( Q ) w = dequeue( Q ) for each unvisited vertex u adjacent to w Visit u and mark it as visited Enqueue( u, Q )

CISC 235 Topic 924 Breadth-First Search on an Undirected, Connected Graph

CISC 235 Topic 925 Depth-First Search dfs( vertex v ) Visit v and mark it as visited for each unvisited vertex u adjacent to v dfs( v )

Analysis of BFS & DFS Let n = | V | Let m = | E | CISC 235 Topic 926

CISC 235 Topic 927 Application: Java Garbage Collection C & C++: Programmer must explicitly allocate and deallocate memory space for objects - source of errors Java: Garbage collection deallocates memory space for objects no longer used. How?

CISC 235 Topic 928 Mark-Sweep Garbage Collection Algorithm Suspend all other running threads. Trace through the Java stacks of currently running threads and mark as “live” all of the “root” objects. Traverse each object in the heap that is active, by starting at each root object, and mark it as “live”. Scan through the entire memory heap and reclaim any space that has not been marked.

Algorithms Related to BFS & DFS How could we test whether an undirected graph G is connected? How could we compute the connected components of G? How could we compute a cycle in G or report that it has no cycle? How could we compute a path between any two vertices, or report that no such path exists? How could we compute for every vertex v of G, the minimum number of edges of any path between s and v? CISC 235 Topic 929