Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 27 Graphs and Applications Part1. Non-weighted Graphs

Similar presentations


Presentation on theme: "Chapter 27 Graphs and Applications Part1. Non-weighted Graphs"— Presentation transcript:

1 Chapter 27 Graphs and Applications Part1. Non-weighted Graphs

2 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).

3 Modeling Using Graphs

4 Weighted Graph Animation

5 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. 

6 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

7 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

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

9 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,

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

11 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 !

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

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

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

15 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) ); …

16 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

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

18 Modeling Graphs

19 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.

20 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); }

21 DFS Depth-First Search Example
Begin at node 0

22 DFS Depth-First Search Example

23 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.

24 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.

25 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; }

26 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

27 BFS Breadth-First Search Example

28 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.

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

30 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); }

31 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);

32 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

33 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

34 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 }

35 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.

36 Case: The Nine Tail Problem

37 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.

38 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.


Download ppt "Chapter 27 Graphs and Applications Part1. Non-weighted Graphs"

Similar presentations


Ads by Google