Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS203 Lecture 15. Modeling Using Graphs 2 For purposes of graph theory, a graph is a mathematical structure used to model pairwise relations between objects.

Similar presentations


Presentation on theme: "CS203 Lecture 15. Modeling Using Graphs 2 For purposes of graph theory, a graph is a mathematical structure used to model pairwise relations between objects."— Presentation transcript:

1 CS203 Lecture 15

2 Modeling Using Graphs 2 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

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

4 Directed vs Undirected Graph 4 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.

5 Basic Graph Terminology 5 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

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

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

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

9 Representing Edges: Adjacency Matrix 9 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 };

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

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

12 Representing Adjacency Edge List Using ArrayList 12 List > neighbors = new ArrayList<>(); neighbors.add(new ArrayList ()); // 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 ()); 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));

13 Modeling Graphs 13

14 14

15 Graph Visualization 15

16 Spanning Tree 16 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

17 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 17

18 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. 18 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); }

19 Depth-First Search Example 19

20 Depth-First Search Example 20

21 Applications of the DFS 21  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.

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

23 Breadth-First Search Algorithm 23 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; }

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

25 Breadth-First Search Example 25

26 Applications of the BFS 26  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.


Download ppt "CS203 Lecture 15. Modeling Using Graphs 2 For purposes of graph theory, a graph is a mathematical structure used to model pairwise relations between objects."

Similar presentations


Ads by Google