import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class SearchAlgorithms {
public static <T> void DepthFirstSearch(Graph<T> graph, Vertex<T> sourceVertex, boolean reverseNeighbours) {
for (Vertex<T> vertex : graph.Vertices()) {
vertex.Parent = null;
vertex.Distance = 0;
vertex.Visited = false;
}
Stack<Vertex<T>> stack = new Stack<Vertex<T>>();
stack.push(sourceVertex);
while (stack.size() > 0) {
Vertex<T> vertex = stack.pop();
ArrayList<Vertex<T>> neighbours = graph.GetAdjacentVertices(vertex);
if (reverseNeighbours) {
Collections.reverse(neighbours);
//neighbours.Reverse();
}
for (Vertex<T> neighbour : neighbours) {
if (!neighbour.Visited) {
neighbour.Parent = vertex;
neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
neighbour.Visited = true;
stack.push(neighbour);
}
}
vertex.Visited = true;
}
}
public static <T> ArrayList<Vertex<T>> DepthFirstSearchWithGoal(Graph<T> graph, Vertex<T> sourceVertex,
Vertex<T> goalVertex, boolean reverseNeighbours) {
if (sourceVertex.equals(goalVertex)) {
ArrayList<Vertex<T>> res = new ArrayList<Vertex<T>>();
res.add(sourceVertex);
return res;
}
for (Vertex<T> vertex : graph.Vertices()) {
vertex.Parent = null;
vertex.Distance = 0;
vertex.Visited = false;
}
Stack<Vertex<T>> stack = new Stack<Vertex<T>>();
stack.push(sourceVertex);
while (stack.size() > 0) {
Vertex<T> vertex = stack.pop();
ArrayList<Vertex<T>> neighbours = graph.GetAdjacentVertices(vertex);
if (reverseNeighbours) {
Collections.reverse(neighbours);
}
for (Vertex<T> neighbour : neighbours) {
if (!neighbour.Visited) {
neighbour.Parent = vertex;
neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
neighbour.Visited = true;
if (neighbour.equals(goalVertex)) {
return GetPathToSource(neighbour);
}
stack.push(neighbour);
}
}
vertex.Visited = true;
}
// No path found
return null;
}
public static <T> ArrayList<Vertex<T>> GetPathToSource(Vertex<T> from) {
ArrayList<Vertex<T>> path = new ArrayList<Vertex<T>>();
Vertex<T> next = from;
while (next != null) {
path.add(next);
next = next.Parent;
}
return path;
}
}
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
System.out.println("===========BFS=============");
Vertex<String> v11 = new Vertex<String>("v1");
Vertex<String> v22 = new Vertex<String>("v2");
Vertex<String> v33 = new Vertex<String>("v3");
Vertex<String> v44 = new Vertex<String>("v4");
Vertex<String> v55 = new Vertex<String>("v5");
Vertex<String> v66 = new Vertex<String>("v6");
Vertex<String> v77 = new Vertex<String>("v7");
ArrayList<Vertex<String>> vertices1 = new ArrayList<Vertex<String>>();
//vertices.clear();
vertices1.add(v11);
vertices1.add(v22);
vertices1.add(v33);
vertices1.add(v44);
vertices1.add(v55);
vertices1.add(v66);
vertices1.add(v77);
Graph<String> graph1 = new Graph<String>(vertices1);
graph1.CreateUndirectedEdge(v44, v55, 0);
graph1.CreateUndirectedEdge(v44, v22, 0);
graph1.CreateUndirectedEdge(v44, v11, 0);
graph1.CreateUndirectedEdge(v55, v66, 0);
graph1.CreateUndirectedEdge(v22, v55, 0);
graph1.CreateUndirectedEdge(v22, v77, 0);
graph1.CreateUndirectedEdge(v22, v11, 0);
graph1.CreateUndirectedEdge(v11, v33, 0);
graph1.CreateUndirectedEdge(v11, v77, 0);
graph1.CreateUndirectedEdge(v77, v66, 0);
System.out.println(graph1.toString());
SearchAlgorithms.BreadthFirstSearch(graph1, v44);
ArrayList<Vertex<String>> fromV6 = SearchAlgorithms.GetPathToSource(v66);
System.out.println("Printing path from v6");
for (Vertex<String> vertex : fromV6) {
System.out.println(vertex);
}
ArrayList<Vertex<String>> path = SearchAlgorithms.BreadthFirstSearchWithGoal(graph1, v66, v33);
System.out.println("Printing path from v3 to v6");
for (Vertex<String> vertex : path) {
System.out.println(vertex);
}
System.out.println("===========DFS=============");
System.out.println(graph1.toString());
SearchAlgorithms.DepthFirstSearch(graph1, v44, true);
ArrayList<Vertex<String>> fromV66 = SearchAlgorithms.GetPathToSource(v66);
System.out.println("Printing path from v6");
for (Vertex<String> vertex : fromV66) {
System.out.println(vertex);
}
ArrayList<Vertex<String>> path1 = SearchAlgorithms.DepthFirstSearchWithGoal(graph1, v66, v33, true);
System.out.println("Printing path from v3 to v6");
for (Vertex<String> vertex : path1) {
System.out.println(vertex);
}
}
}
Graph:
v1 Edge to: v2(w=1.0) v3(w=1.0) v4(w=1.0) v7(w=1.0)
v2 Edge to: v1(w=1.0) v4(w=1.0) v5(w=1.0) v7(w=1.0)
v3 Edge to: v1(w=1.0)
v4 Edge to: v1(w=1.0) v2(w=1.0) v5(w=1.0)
v5 Edge to: v2(w=1.0) v4(w=1.0) v6(w=1.0)
v6 Edge to: v5(w=1.0) v7(w=1.0)
v7 Edge to: v1(w=1.0) v2(w=1.0) v6(w=1.0)
Printing path from v6
{Vertex} v6
{Vertex} v7
{Vertex} v1
{Vertex} v4
Printing path from v3 to v6
{Vertex} v3
{Vertex} v1
{Vertex} v2
{Vertex} v5
{Vertex} v6
Комментариев нет:
Отправить комментарий