-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBridgeFinder.java
More file actions
79 lines (67 loc) · 2.72 KB
/
BridgeFinder.java
File metadata and controls
79 lines (67 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Author: Kevin Richardson <[email protected]>
* Date: 2011-Dec-8
* Time: 5:51 PM
*
* Given a graph file, this class detects and outputs any bridges in the structure (should a
* bridge or bridges exist) in O(|V| + |E|).
*/
import java.util.Scanner;
public class BridgeFinder
{
static BridgeNodeGraph graph;
static int depthCount = 0;
static int bridgeCount = 0;
public static void main(String[] args)
{
// Acquire the user's graph file.
Scanner scan = new Scanner(System.in);
System.out.println("Graph bridge finder\n====================");
System.out.print("Name of graph file: ");
graph = new BridgeNodeGraph(scan.nextLine());
// Detect any bridges in the graph starting at node 0.
detectEdges(graph, 0);
}
// Given a BridgeNodeGraph, this function prints out any bridges that exist in the structure.
// If no bridges are found, the method will tell the user.
// The method will start examining at the specified node.
public static void detectEdges(BridgeNodeGraph graph, int startNode)
{
depthFirstSearch(graph.connections.get(startNode), null);
System.out.println("Number of bridges found: " + bridgeCount);
}
// Run DFS on the graph from the specified vertex.
// Parent tracks the node from which this vertex is being visited.
public static void depthFirstSearch(BridgeNode vertex, BridgeNode parent)
{
vertex.visited = true;
vertex.depth = depthCount++;
// Examine the connections to this node.
for(BridgeNode connection : vertex.connections)
{
// Don't examine the parent again.
if(connection != parent)
{
// If unvisited, run DFS from this connection.
if(!connection.visited)
{
// vertex - connection is a forward edge.
depthFirstSearch(connection, vertex);
// A bridge exists when the depth values of two connecting nodes differs after DFS.
if(connection.depth > vertex.depth)
{
System.out.println("A bridge was detected: " + vertex.getValue() + "-" + connection.getValue());
bridgeCount++;
}
// Update depth values to correspond to the DFS.
vertex.depth = Math.min(vertex.depth, connection.depth);
}
// Otherwise, vertex - connection is a back edge. Update vertex's depth value.
else
{
vertex.depth = Math.min(vertex.depth, connection.depth);
}
}
}
}
}