0% found this document useful (0 votes)
3 views29 pages

Chapter 10 Graph

The document discusses graph theory, including basic terminologies, representations, and traversal algorithms such as depth-first search (DFS) and breadth-first search (BFS). It explains various ways to represent graphs, such as adjacency matrices and edge lists, and outlines the applications of DFS in detecting connectivity and cycles within graphs. Additionally, it provides examples of graph implementation in Java, demonstrating how to create and traverse graphs programmatically.

Uploaded by

hkk7kfv9np
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views29 pages

Chapter 10 Graph

The document discusses graph theory, including basic terminologies, representations, and traversal algorithms such as depth-first search (DFS) and breadth-first search (BFS). It explains various ways to represent graphs, such as adjacency matrices and edge lists, and outlines the applications of DFS in detecting connectivity and cycles within graphs. Additionally, it provides examples of graph implementation in Java, demonstrating how to create and traverse graphs programmatically.

Uploaded by

hkk7kfv9np
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Graphs and Applications

1
Modeling Using Graphs

2
Seven Bridges of Königsberg
A

C
D
Island 1
Island 2

C D

3
Basic Graph Terminologies

What is a graph? G=(V, E)


Define a graph
Directed vs. undirected graphs
Weighted vs. unweighted graphs
Adjacent vertices
Incident
Degree
Neighbor
loop
4
Directed vs Undirected Graph

Peter Jane

Cindy Mark

Wendy

5
Basic Graph Terminologies

Parallel edge
Simple graph
Complete graph
Spanning tree

A D
A D

B E
B E

C
C

6
Representing Graphs
Representing Vertices
Representing Edges: Edge Array
Representing Edges: Edge Objects
Representing Edges: Adjacency Matrices
Representing Edges: Adjacency Lists

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

public class City {

List<String> vertices;

8
Representing Edges: Edge Array
int[][] edges = {{0, 1}, {0, 3} {0, 5}, {1, 0}, {1, 2}, … };

9
Representing Edges: Edge Object
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)); …

10
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
};
11
Representing Edges: Adjacency Vertex List
List<Integer>[] neighbors = new List[12];
Seattle neighbors[0] 1 3 5
San Francisco neighbors[1] 0 2 3
Los Angeles neighbors[2] 1 3 4 10
Denver neighbors[3] 0 1 2 4 5
Kansas City neighbors[4] 2 3 5 7 8 10
Chicago neighbors[5] 0 3 4 6 7
Boston neighbors[6] 5 7
New York neighbors[7] 4 5 6 8

Atlanta neighbors[8] 4 7 9 10 11
Miami neighbors[9] 8 11
Dallas neighbors[10] 2 4 8 11
Houston neighbors[11] 8 9 10

List<List<Integer>> neighbors = new ArrayList<>();


12
Representing Edges: Adjacency Edge List
List<Edge>[] neighbors = new List[12];
Seattle neighbors[0] Edge(0, 1) Edge(0, 3) Edge(0, 5)
San Francisco neighbors[1] Edge(1, 0) Edge(1, 2) Edge(1, 3)
Los Angeles neighbors[2] Edge(2, 1) Edge(2, 3) Edge(2, 4) Edge(2, 10)
Denver neighbors[3] Edge(3, 0) Edge(3, 1) Edge(3, 2) Edge(3, 4) Edge(3, 5)
Kansas City neighbors[4] Edge(4, 2) Edge(4, 3) Edge(4, 5) Edge(4, 7) Edge(4, 8) Edge(4, 10)
Chicago neighbors[5] Edge(5, 0) Edge(5, 3) Edge(5, 4) Edge(5, 6) Edge(5, 7)
Boston neighbors[6] Edge(6, 5) Edge(6, 7)
New York neighbors[7] Edge(7, 4) Edge(7, 5) Edge(7, 6) Edge(7, 8)

Atlanta neighbors[8] Edge(8, 4) Edge(8, 7) Edge(8, 9) Edge(8, 10) Edge(8, 11)

Miami neighbors[9] Edge(9, 8) Edge(9, 11)


Dallas neighbors[10] Edge(10, 2) Edge(10, 4) Edge(10, 8) Edge(10, 11)

Houston neighbors[11] Edge(11, 8) Edge(11, 9) Edge(11, 10)

13
Representing Adjacency Edge List Using ArrayList
List<ArrayList<Edge>> neighbors = new ArrayList<>();
neighbors.add(new ArrayList<Edge>());
neighbors.get(0).add(new Edge(0, 1));
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));
14
Modeling Graphs

Graph UnweightedGraph WeightedGraph

15
«interface» The generic type V is the type for vertices.
Graph<V>
+getSize(): int Returns the number of vertices in the graph.
+getVertices(): List<V> Returns the vertices in the graph.
+getVertex(index: int): V Returns the vertex object for the specified vertex index.
+getIndex(v: V): int Returns the index for the specified vertex.
+getNeighbors(index: int): List<Integer> Returns the neighbors of vertex with the specified index.
+getDegree(index: int): int Returns the degree for a specified vertex index.
+printEdges(): void
Prints the edges.
+clear(): void Clears the graph.
+addVertex(v: V): boolean Returns true if v is added to the graph. Returns false if v
is already in the graph.
+addEdge(u: int, v: int): boolean Adds an edge from u to v to the graph throws
IllegalArgumentException if u or v is invalid. Returns
true if the edge is added and false if (u, v) is already in
the graph.
Adds an edge into the adjacency edge list.
+addEdge(e: Edge): boolean
+remove(v: V): boolean Removes a vertex from the graph.
+remove(u: int, v: int): boolean Removes an edge from the graph.
+dfs(v: int): UnWeightedGraph<V>.SearchTree Obtains a depth-first search tree starting from v.
+bfs(v: int): UnWeightedGraph<V>.SearchTree Obtains a breadth-first search tree starting from v.
.

UnweightedGraph<V>
#vertices: List<V> Vertices in the graph.
#neighbors: List<List<Edge>> Neighbors for each vertex in the graph.
+UnweightedGraph() Constructs an empty graph.
+UnweightedGraph(vertices: V[], edges: Constructs a graph with the specified edges and vertices
int[][]) stored in arrays.
+UnweightedGraph(vertices: List<V>, Constructs a graph with the specified edges and vertices
edges: List<Edge>)
stored in lists.
+UnweightedGraph(edges: int[][], Constructs a graph with the specified edges in an array
numberOfVertices: int) and the integer vertices 1, 2, ....
+UnweightedGraph(edges: List<Edge>, Constructs a graph with the specified edges in a list and
numberOfVertices: int) the integer vertices 1, 2, ….

16
Graph Traversals
Depth-first search and breadth-first search

Both traversals result in a spanning tree, which can be


modeled using a class.
UnweightedGraph<V>.SearchTree

-root: int The root of the tree.


-parent: int[] The parents of the vertices.
-searchOrder: List<Integer> The orders for traversing the vertices.

+SearchTree(root: int, parent: Constructs a tree with the specified root, parent, and
int[], searchOrder: List<Integer>) searchOrder.
+getRoot(): int Returns the root of the tree.
+getSearchOrder(): List<Integer> Returns the order of vertices searched.
+getParent(index: int): int Returns the parent for the specified vertex index.
+getNumberOfVerticesFound(): int Returns the number of vertices searched.
+getPath(index: int): List<V> Returns a list of vertices from the specified vertex index
to the root.
+printPath(index: int): void Displays a path from the root to the specified vertex.
+printTree(): void
Displays tree with the root and all edges.

17
Depth-First Search
The depth-first search of a graph is like the depth-first search of a
tree discussed in §25.2.3, “Tree Traversal.” 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);
}
18
}
Depth-First Search Example

19
Depth-First Search Example
Seattle

2097 Boston
983
Chicago
1331 214
807
1003 New York
787
Denver
533
1267 1260
599
888
San Francisco 1015 Kansas City

381 1663
864

496
Los Angeles 1435 Atlanta
781
810
Dallas
661
239

Houston 1187

Miami

20
public static void main(String[] args) {
String[] vertices = {"Seattle", "San Francisco", "Los Angeles",
"Denver", "Kansas City", "Chicago", "Boston", "New York",
"Atlanta", "Miami", "Dallas", "Houston"};

int[][] edges = {
{0, 1}, {0, 3}, {0, 5},
{1, 0}, {1, 2}, {1, 3},
{2, 1}, {2, 3}, {2, 4}, {2, 10},
{3, 0}, {3, 1}, {3, 2}, {3, 4}, {3, 5},
{4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 8}, {4, 10},
{5, 0}, {5, 3}, {5, 4}, {5, 6}, {5, 7},
{6, 5}, {6, 7},
{7, 4}, {7, 5}, {7, 6}, {7, 8},
{8, 4}, {8, 7}, {8, 9}, {8, 10}, {8, 11},
{9, 8}, {9, 11},
{10, 2}, {10, 4}, {10, 8}, {10, 11},
{11, 8}, {11, 9}, {11, 10}
};

21
Graph<String> graph = new UnweightedGraph<>(vertices, edges);
UnweightedGraph<String>.SearchTree dfs =
graph.dfs(graph.getIndex("Chicago")); // Get a dfs starting at Chicago

java.util.List<Integer> searchOrders = dfs.getSearchOrder();


System.out.println(dfs.getNumberOfVerticesFound() +
" vertices are searched in this DFS order:");
for (int i = 0; i < searchOrders.size(); i++)
System.out.print(graph.getVertex(searchOrders.get(i)) + " ");
System.out.println();

for (int i = 0; i < searchOrders.size(); i++)


if (dfs.getParent(i) != -1)
System.out.println("parent of " + graph.getVertex(i) +
" is " + graph.getVertex(dfs.getParent(i)));
}

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


23
Breadth-First Search
The breadth-first traversal of a graph is like the breadth-
first traversal of a tree discussed in §25.2.3, “Tree
Traversal.” 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.

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

26
Breadth-First Search Example
Seattle

2097 Boston
983
Chicago
1331 214
807
1003 New York
787
Denver
533
1267 1260
599
888
San Francisco 1015 Kansas City

381 1663
864

496
Los Angeles 1435 Atlanta
781
810
Dallas
661
239

Houston 1187

Miami

27
public class TestBFS {
public static void main(String[] args) {
String[] vertices = {"Seattle", "San Francisco", "Los Angeles",
"Denver", "Kansas City", "Chicago", "Boston", "New York",
"Atlanta", "Miami", "Dallas", "Houston"};

int[][] edges = {
{0, 1}, {0, 3}, {0, 5},
{1, 0}, {1, 2}, {1, 3},
{2, 1}, {2, 3}, {2, 4}, {2, 10},
{3, 0}, {3, 1}, {3, 2}, {3, 4}, {3, 5},
{4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 8}, {4, 10},
{5, 0}, {5, 3}, {5, 4}, {5, 6}, {5, 7},
{6, 5}, {6, 7},
{7, 4}, {7, 5}, {7, 6}, {7, 8},
{8, 4}, {8, 7}, {8, 9}, {8, 10}, {8, 11},
{9, 8}, {9, 11},
{10, 2}, {10, 4}, {10, 8}, {10, 11},
{11, 8}, {11, 9}, {11, 10}
};

28
Graph<String> graph = new UnweightedGraph<>(vertices, edges);
UnweightedGraph<String>.SearchTree bfs =
graph.bfs(graph.getIndex("Chicago")); // Get a dfs starting at Chicago

java.util.List<Integer> searchOrders = bfs.getSearchOrder();


System.out.println(bfs.getNumberOfVerticesFound() +
" vertices are searched in this order:");
for (int i = 0; i < searchOrders.size(); i++)
System.out.println(graph.getVertex(searchOrders.get(i)));

for (int i = 0; i < searchOrders.size(); i++)


if (bfs.getParent(i) != -1)
System.out.println("parent of " + graph.getVertex(i) +
" is " + graph.getVertex(bfs.getParent(i)));
}
}

29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy