CS203 Lecture 15.

Slides:



Advertisements
Similar presentations
Chapter 28 Weighted Graphs and Applications
Advertisements

Mathematical Preliminaries
Analysis of Algorithms
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
ABC Technology Project
Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
1 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I.
© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
CS1022 Computer Programming & Principles
25 seconds left…...
We will resume in: 25 Minutes.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Graph Theory.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
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
Depth-First Search Lecture 21: Graph Traversals
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.
Graphs Upon completion you will be able to:
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.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
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.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Chapter 28 Graphs and Applications Part1. Non-weighted Graphs CIS265/506 Cleveland State University – Prof. Victor Matos Adapted from: Introduction to.
Breadth-First Search (BFS)
Graphs Chapter 20.
Chapter 27 Graphs and Applications Part1. Non-weighted Graphs
Chapter 28 Graphs and Applications
Csc 2720 Instructor: Zhuojun Duan
Lecture 12 Graph Algorithms
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),
Subgraphs, Connected Components, Spanning Trees
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

CS203 Lecture 15

Modeling Using Graphs For purposes of graph theory, a graph is a mathematical structure used to model pairwise relations between objects. Graphs are represented by vertices that are connected by edges

Seven Bridges of Königsberg Objective: cross each of the seven bridges exactly once. Crossing a bridge is atomic; there are no partial crossings

Directed vs Undirected Graph This is from Liang. I am not sure what kind of relationship is going on between these five people, but this is an example of a directed graph. The seven bridges graph is undirected; you can cross any bridge in either direction.

Basic Graph Terminology Adjacent vertices: vertices that are directly connected by one or more edges Parallel edges: multiple edges that connect the same vertices Simple graph: no loops or parallel edges Complete graph: one with connections between each two edges Connected graph: one in which each vertex can be reached from every other vertex Cycle: a roundtrip returning to the same vertex

Representing Vertices String[] vertices = {“Seattle“, “San Francisco“, “Los Angeles”, “Denver”, “Kansas City”, “Chicago”, … }; City[] vertices = {city0, city1, … }; List<String> vertices;

Representing Edges: Edge Array int[][] edges = {{0, 1}, {0, 3}, {0, 5}, {1, 0}, {1, 2}, … }; Each pair is represented as a *row* in the 2D array: 0 1 0 3 0 5 1 0 1 2

Representing Edges: List of Edge Objects public class Edge { int u, v; public Edge(int u, int v) { this.u = u; this.v = v; } List<Edge> list = new ArrayList<>(); list.add(new Edge(0, 1)); list.add(new Edge(0, 3)); …

Representing Edges: Adjacency Matrix int[][] adjacencyMatrix = { {0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0}, // Seattle {1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // San Francisco {0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // Los Angeles {1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0}, // Denver {0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0}, // Kansas City {1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0}, // Chicago {0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, // Boston {0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0}, // New York {0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1}, // Atlanta {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1}, // Miami {0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1}, // Dallas {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0} // Houston };

Representing Edges: Adjacency Vertex List List<Integer>[] neighbors = new List[12]; List<List<Integer>> neighbors = new ArrayList<>();

Representing Edges: Adjacency Edge List List<Edge>[] neighbors = new List[12];

Representing Adjacency Edge List Using ArrayList List<ArrayList<Edge>> neighbors = new ArrayList<>(); neighbors.add(new ArrayList<Edge>()); // instantiate the first inner list neighbors.get(0).add(new Edge(0, 1)); // add an Edge to the first inner list neighbors.get(0).add(new Edge(0, 3)); neighbors.get(0).add(new Edge(0, 5)); neighbors.add(new ArrayList<Edge>()); neighbors.get(1).add(new Edge(1, 0)); neighbors.get(1).add(new Edge(1, 2)); neighbors.get(1).add(new Edge(1, 3)); ... neighbors.get(11).add(new Edge(11, 8)); neighbors.get(11).add(new Edge(11, 9)); neighbors.get(11).add(new Edge(11, 10));

Modeling Graphs

Graph Visualization

Spanning Tree A Spanning Tree is a connected, undirected graph consisting of all the vertices and some or all of the edges of a graph, arranged with no cycles

Graph Traversals Depth-first search and breadth-first search Both traversals result in a spanning tree, which can be modeled using a class. Unlike previous diagrams of this type, this one is not already implemented in the Java API

Depth-First Search The depth-first search of a graph is like the depth-first search of a tree. In the case of a tree, the search starts from the root. In a graph, the search can start from any vertex. Input: G = (V, E) and a starting vertex v Output: a DFS tree rooted at v Tree dfs(vertex v) { visit v; for each neighbor w of v if (w has not been visited) { set v as the parent for w; dfs(w); }

Depth-First Search Example

Depth-First Search Example

Applications of the DFS Detecting whether a graph is connected. Search the graph starting from any vertex. If the number of vertices searched is the same as the number of vertices in the graph, the graph is connected. Otherwise, the graph is not connected. Detecting whether there is a path between two vertices. Finding a path between two vertices. Finding all connected components. A connected component is a maximal connected subgraph in which every pair of vertices are connected by a path. Detecting whether there is a cycle in the graph. Finding a cycle in the graph.

Breadth-First Search The breadth-first traversal of a graph is like the breadth-first traversal of a tree. The nodes are visited level by level. First the root is visited, then all the children of the root, then the grandchildren of the root from left to right, and so on.

Breadth-First Search Algorithm Input: G = (V, E) and a starting vertex v Output: a BFS tree rooted at v bfs(vertex v) { create an empty queue for storing vertices to be visited; add v into the queue; mark v visited; while the queue is not empty { dequeue a vertex, say u, from the queue process u; for each neighbor w of u if w has not been visited { add w into the queue; set u as the parent for w; mark w visited; }

Breadth-First Search Example Queue: 0 isVisited[0] = true Queue: 1 2 3 isVisited[1] = true, isVisited[2] = true, isVisited[3] = true Queue: 2 3 4 isVisited[4] = true

Breadth-First Search Example

Applications of the BFS Detecting whether a graph is connected. A graph is connected if there is a path between any two vertices in the graph. Detecting whether there is a path between two vertices. Finding a shortest path between two vertices. You can prove that the path between the root and any node in the BFS tree is the shortest path between the root and the node. Finding all connected components. A connected component is a maximal connected subgraph in which every pair of vertices are connected by a path. Detecting whether there is a cycle in the graph. Finding a cycle in the graph. Testing whether a graph is bipartite. A graph is bipartite if the vertices of the graph can be divided into two disjoint sets such that no edges exist between vertices in the same set.