Chapter 27 Graphs and Applications Part1. Non-weighted Graphs

Slides:



Advertisements
Similar presentations
CSE 211 Discrete Mathematics
Advertisements

CS203 Lecture 15.
22C:19 Discrete Math Graphs Fall 2010 Sukumar Ghosh.
Introduction to Graph Theory Instructor: Dr. Chaudhary Department of Computer Science Millersville University Reading Assignment Chapter 1.
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.
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.
Data Structures Using C++
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Discrete Structures Chapter 7B Graphs Nurul Amelina Nasharuddin Multimedia Department.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs. Graph A “graph” is a collection of “nodes” that are connected to each other Graph Theory: This novel way of solving problems was invented by a.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
Graph Theory Topics to be covered:
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 30 Graphs and Applications.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Chapter 28 Graphs and Applications Part1. Non-weighted Graphs CIS265/506 Cleveland State University – Prof. Victor Matos Adapted from: Introduction to.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
Graphs Chapter 20.
Chapter 28 Graphs and Applications
Data Structures Graphs - Terminology
Data Structures 13th Week
Java Programming: Program Design Including Data Structures
Weighted Graphs: Networks
Csc 2720 Instructor: Zhuojun Duan
Graph theory Definitions Trees, cycles, directed graphs.
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
Introduction to Graphs
Data Structures Graphs: Networks CIS 265/506 - Chapter 14 Graphs.
Graphs: As a mathematics branch
CS120 Graphs.
Data Structures and Algorithms for Information Processing
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Operations And Representation
CSE 421: Introduction to Algorithms
Graph & BFS.
Introduction to Graph Theory Euler and Hamilton Paths and Circuits
Graph Theory.
Graphs Chapter 13.
Genome Assembly.
Search Related Algorithms
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.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graphs Part 2 Adjacency Matrix
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Spanning Trees Longin Jan Latecki Temple University based on slides by
Richard Anderson Autumn 2016 Lecture 5
COMP108 Algorithmic Foundations Graph Theory
Euler and Hamilton Paths
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Spanning Trees Longin Jan Latecki Temple University based on slides by
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Presentation transcript:

Chapter 27 Graphs and Applications Part1. Non-weighted Graphs

Objectives To model real-world problems using graphs and explain the Seven Bridges of Königsberg problem (§27.1). To describe the graph terminologies: vertices, edges, simple graphs, weighted/unweighted graphs, and directed/undirected graphs (§27.2). To represent vertices and edges using lists, adjacent matrices, and adjacent lists (§27.3). To model graphs using the Graph interface, the AbstractGraph class, and the UnweightedGraph class (§27.4). To represent the traversal of a graph using the AbstractGraph.Tree class (§27.5). To design and implement depth-first search (§27.6). To design and implement breadth-first search (§27.7). To solve the nine-tail problem using breadth-first search (§27.8). To solve the Knight’s Tour problem by reducing it to a Hamiltonian path problem (§27.9).

Modeling Using Graphs

Weighted Graph Animation www.cs.armstrong.edu/liang/animation/GraphLearningTool.html

Seven Bridges of Königsberg The city of Konigsberg is connected to two islands through seven bridges. The problem was to find a walk through the city that would cross each bridge once and only once. 

Seven Bridges of Königsberg No solution!! Def. A path that traverses all bridges and also has the same starting and ending is called an Eulerian circuit. Such a circuit exists if, and only if, the graph is connected, and there are no nodes of odd degree at all. Leonhard Euler, 1707

Seven Bridges of Königsberg Eulerian Paths / Trails Def. For a graph to have an Euler path or trail (each edge is visited once) it is necessary that no more than two vertices have an odd degree; therefore the Königsberg graph is not Eulerian. Note: a trail does not require to begin and end in the same vertex. Leonhard Euler, 1707

Basic Graph Terminology What is a graph? Directed vs. undirected graphs Weighted vs. non-weighted graphs Adjacent vertices Incident Degree Neighbor loop Leonhard Euler, 1707-1785

Basic Graph Terminology What is a graph? A graph G(V, E) is a representation of a set of vertices V and edges E. Two vertices v1 and v2 in V are related iff there is and edge e12 in E connecting v1 and v2. Leonhard Euler, 1707-1785

Basic Graph Terminology Node, vertice, vertex Parallel edge Loops Complete graph Spanning tree Simple graph (connected, undirected, no loops, no parallel weighted)

Basic Graph Terminology Spanning tree T of a connected, undirected graph G is a tree composed of all the vertices and some (or perhaps all) of the edges of G. A graph may have several Spanning trees !

Java: Representing Graphs Vertices Representing Edges: Edge Array Edge Objects (POJO) Adjacency Matrices Adjacency Lists

Representing Vertices String[ ] vertices = {“Seattle“, “San Francisco“, … }; City[ ] vertices = {city0, city1, … }; public class City { private String cityName; private int population; ... } List<String> vertices;

Representing Edges: Edge Array int[][] edges = {{0, 1}, {0, 3} {0, 5}, {1, 0}, {1, 2}, … }; There is an edge between city0 and city1

Representing Edges: Edge Object public class Edge { int v1, v2; public Edge(int v1, int v2) { this.v1 = v1; this.v 2 = v2; } List<Edge> list = new ArrayList<Edge>(); 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 }; Entry adjacencyMatrix[i][j] is 1 if there is an edge connecting city-i and city-j

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

Modeling Graphs

Graph Traversals Depth-first search (DFS) and breadth-first search (BFS) Both traversals result in a spanning tree, which can be modeled using a class.

Depth-First Search (DFS) In the case of a tree, DFS search starts from the root (similar to pre-order navigation). In a graph, the search can start from any vertex. dfs(vertex v) { visit v; for each neighbor w of v if (w has not been visited) { dfs(w); }

DFS Depth-First Search Example Begin at node 0

DFS 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 / 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 / Finding a cycle in a graph.

Breadth-First Search (BFS) With 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.

BFS Breadth-First Search Algorithm 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; mark w visited; }

BFS 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

BFS 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 /Finding whether there is 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.

Programming Excersice Represent the following graph. Traverse using DFS and BFS

BFS & DFS Programming Exercise public static void main(String[] args) { Graph<String> graph = new Graph<String>(); graph.addVertice("V0"); graph.addVertice("V1"); graph.addVertice("V2"); graph.addVertice("V3"); graph.addVertice("V4"); graph.addVertice("V5"); graph.addEdge(0,1); graph.addEdge(1,2); graph.addEdge(1,3); graph.addEdge(1,4); graph.addEdge(4,5); graph.addEdge(2,5); graph.addEdge(3,5); graph.showData(); System.out.println("\nDFS >>> "); graph.clearVisitedMarkers(); graph.dfs(0); System.out.println("\nBFS >>> "); graph.bfs(0); }

BFS & DFS Programming Exercise public class Graph <E> { private ArrayList<E> vertices; private ArrayList< ArrayList<Integer> > neighbors; private ArrayList<Boolean> visitedMarkers; public Graph() { vertices = new ArrayList<E>(); neighbors = new ArrayList < ArrayList<Integer> >(); visitedMarkers = new ArrayList<Boolean>(); } public void addVertice(E newVertice){ vertices.add(newVertice); visitedMarkers.add(false); neighbors.add(new ArrayList<Integer>() ); public void addEdge( int v1, int v2){ ArrayList<Integer> adjacent1 = neighbors.get(v1); if (!adjacent1.contains(v2)) neighbors.get(v1).add(v2); ArrayList<Integer> adjacent2 = neighbors.get(v2); if (!adjacent2.contains(v1)) neighbors.get(v2).add(v1);

BFS & DFS Programming Exercise public void markVerticeAsVisited(int v){ visitedMarkers.set(v, true); } public void clearVisitedMarkers(){ for(int i=0; i<vertices.size(); i++){ visitedMarkers.set(i, false); public void showData() { for (int i=0; i<vertices.size(); i++){ System.out.printf("\nVertice[%d]= %s", i, vertices.get(i)); for (int i=0; i < vertices.size(); i++){ System.out.printf("\nneighbors[%d]: ", i); ArrayList<Integer> adjacent = neighbors.get(i); for (int j=0; j<adjacent.size(); j++){ System.out.printf(" %d,", adjacent.get(j) ); }//showData

BFS & DFS Programming Exercise public void dfs(int v) { visitedMarkers.set(v, true); for(int n : neighbors.get(v)){ if (!visitedMarkers.get(n)){ System.out.printf("[v%d-v%d] ", v, n); dfs(n); } }//dfs

BFS & DFS Programming Excersice public void bfs(int v){ ArrayList<Integer> queue = new ArrayList<Integer>(); clearVisitedMarkers(); visitedMarkers.set(v, true); System.out.printf("BFS >>> starting at: v%d \n", v); queue.add(v); while(queue.size() > 0){ int currentDequeuedVertice = queue.remove(0); ArrayList<Integer> adjacents = neighbors.get(currentDequeuedVertice); for(int i=0; i<adjacents.size(); i++){ int adjacentNode = adjacents.get(i); if (!visitedMarkers.get(adjacentNode)){ System.out.printf("[v%d-v%d] ", currentDequeuedVertice, adjacentNode); queue.add(adjacentNode); visitedMarkers.set(adjacentNode, true); }//if }//for }//while }//bfs }

Case: The Nine Tail Problem Nine coins are placed in a three by three matrix with some face up and some face down. A legal move is to take any coin that is face up and reverse it, together with the coins adjacent to it (this does not include coins that are diagonally adjacent). Your task is to find the minimum number of the moves that lead to all coins face down.

Case: The Nine Tail Problem

Finding Eulerian Paths An Eulerian path traverses each edge of a connected graph G exactly once. It exists only if there are no more than two odd nodes. Pick an odd vertex to start. From that vertex move to a node using an “unvisited” edge. Mark that edge as “visited” (see note) Try to repeat 2-4 until all edges have been traversed More than two odd nodes ⟶ no Euler Path Note: DFS algorithm marks nodes as visited.

The Hamiltonian Path Problem Def. A Hamiltonian path in a graph is a path that visits each vertex in the graph exactly once. Def. A Hamiltonian cycle is a cycle that visits each vertex in the graph exactly once and returns to the starting vertex. Notes: Problem is NP-Complete Many applications such as: Scheduling the Traveling Salesman Itinerary.