CS203 Lecture 15. Modeling Using Graphs 2 For purposes of graph theory, a graph is a mathematical...
-
Upload
stone-shute -
Category
Documents
-
view
214 -
download
1
Transcript of CS203 Lecture 15. Modeling Using Graphs 2 For purposes of graph theory, a graph is a mathematical...
![Page 1: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/1.jpg)
CS203Lecture 15
![Page 2: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/2.jpg)
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
![Page 3: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/3.jpg)
Seven Bridges of Königsberg
3
A
C
B
D
Objective: cross each of the seven bridges exactly once.Crossing a bridge is atomic; there are no partial crossings
![Page 4: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/4.jpg)
Directed vs Undirected Graph
4
Peter
Cindy
Wendy
Mark
Jane
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.
![Page 5: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/5.jpg)
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
A
B
C
E
D A
B
C
E
D
![Page 6: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/6.jpg)
Representing Vertices
6
String[] vertices = {“Seattle“, “San Francisco“, “Los Angeles”, “Denver”, “Kansas City”, “Chicago”, … };
City[] vertices = {city0, city1, … };
List<String> vertices;
![Page 7: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/7.jpg)
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:
0 1
0 3
0 5
1 0
1 2
![Page 8: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/8.jpg)
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<Edge> list = new ArrayList<>();
list.add(new Edge(0, 1)); list.add(new Edge(0, 3)); …
![Page 9: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/9.jpg)
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};
![Page 10: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/10.jpg)
Representing Edges: Adjacency Vertex List
10
List<Integer>[] neighbors = new List[12];
List<List<Integer>> neighbors = new ArrayList<>();
neighbors[0]
neighbors[1]
neighbors[2]
neighbors[3]
neighbors[4]
neighbors[5]
neighbors[6]
neighbors[7]
neighbors[8]
neighbors[9]
neighbors[10]
neighbors[11]
Seattle
San Francisco
Los Angeles
Denver
Kansas City
Chicago
Boston
New York
Atlanta
Miami
Dallas
Houston
1 3 5
0 2 3
1 3 4 10
0 1 2 4 5
2 3 5 7 8 10
0 3 4 6 7
5 7
4 5 6 8
4 7 9 10 11
8 11
2 4 8 11
8 9 10
![Page 11: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/11.jpg)
Representing Edges: Adjacency Edge List
11
List<Edge>[] neighbors = new List[12];
neighbors[0]
neighbors[1]
neighbors[2]
neighbors[3]
neighbors[4]
neighbors[5]
neighbors[6]
neighbors[7]
neighbors[8]
neighbors[9]
neighbors[10]
neighbors[11]
Seattle
San Francisco
Los Angeles
Denver
Kansas City
Chicago
Boston
New York
Atlanta
Miami
Dallas
Houston
Edge(0, 1) Edge(0, 3) Edge(0, 5)
Edge(1, 0) Edge(1, 2) Edge(1, 3)
Edge(2, 1) Edge(2, 3) Edge(2, 4) Edge(2, 10)
Edge(3, 0) Edge(3, 1) Edge(3, 2) Edge(3, 4) Edge(3, 5)
Edge(4, 2) Edge(4, 3) Edge(4, 5) Edge(4, 7) Edge(4, 8) Edge(4, 10)
Edge(5, 0) Edge(5, 3) Edge(5, 4) Edge(5, 6) Edge(5, 7)
Edge(6, 5) Edge(6, 7)
Edge(7, 4) Edge(7, 5) Edge(7, 6) Edge(7, 8)
Edge(8, 4) Edge(8, 7) Edge(8, 9) Edge(8, 10) Edge(8, 11)
Edge(9, 8) Edge(9, 11)
Edge(10, 2) Edge(10, 4) Edge(10, 8) Edge(10, 11)
Edge(11, 8) Edge(11, 9) Edge(11, 10)
![Page 12: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/12.jpg)
Representing Adjacency Edge List Using ArrayList
12
List<ArrayList<Edge>> neighbors = new ArrayList<>();neighbors.add(new ArrayList<Edge>()); // instantiate the first inner listneighbors.get(0).add(new Edge(0, 1)); // add an Edge to the first inner listneighbors.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));
![Page 13: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/13.jpg)
Modeling Graphs
13
![Page 14: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/14.jpg)
14
![Page 15: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/15.jpg)
Graph Visualization
15
![Page 16: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/16.jpg)
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
![Page 17: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/17.jpg)
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
![Page 18: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/18.jpg)
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 vOutput: 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);
}
}
![Page 19: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/19.jpg)
Depth-First Search Example
19
![Page 20: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/20.jpg)
Depth-First Search Example
20
Seattle
San Francisco
Los Angeles
Denver
Chicago
Kansas City
Houston
Boston
New York
Atlanta
Miami
661
888
1187
810
Dallas
1331
2097
1003 807
381
1015
1267
1663
1435
239
496
781
864
1260
983
787
214
533
599
![Page 21: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/21.jpg)
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.
![Page 22: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/22.jpg)
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
![Page 23: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/23.jpg)
Breadth-First Search Algorithm
23
Input: G = (V, E) and a starting vertex vOutput: 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;
}
}
}
![Page 24: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/24.jpg)
Breadth-First Search Example
24
Queue: 0
Queue: 1 2 3
Queue: 2 3 4
isVisited[0] = true
isVisited[1] = true, isVisited[2] = true, isVisited[3] = true
isVisited[4] = true
![Page 25: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/25.jpg)
Breadth-First Search Example
25
Seattle
San Francisco
Los Angeles
Denver
Chicago
Kansas City
Houston
Boston
New York
Atlanta
Miami
661
888
1187
810
Dallas
1331
2097
1003 807
381
1015
1267
1663
1435
239
496
781
864
1260
983
787
214
533
599
![Page 26: 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.](https://reader036.fdocuments.in/reader036/viewer/2022081602/551c2688550346ad4f8b5df9/html5/thumbnails/26.jpg)
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.