вторник, 12 декабря 2017 г.

Breadth First Search (BFS)




public class Vertex<T> {

    private int Index;
    private T _data;

    // For keeping track of search
    public Vertex<T> Parent;
    public boolean Visited;
    public float Distance;

    //Constructor
    public Vertex(T data) {
        _data = data;
        Index = -1;
    }

    public Vertex<T> ParentGet() {
        return Parent;
    }

    public void ParentSet(Vertex<T> parent) {
        this.Parent = parent;
    }

    public boolean Visited() {
        return Visited;
    }

    public void VisitedSet(boolean visited) {
        this.Visited = visited;
    }

    public float DistanceGet() {
        return Distance;
    }

    public void DistanceSet(float distance) {
        this.Distance = distance;
    }

    public int IndexGet() {
        return Index;
    }

    public void IndexSet(int Index) {
        this.Index = Index;
    }

    public T Data() {
        return _data;
    }

    @Override
    public String toString() {
        return "{Vertex} " + Data().toString();
    }

}

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class SearchAlgorithms {

    public static <T> void BreadthFirstSearch(Graph<T> graph, Vertex<T> sourceVertex) {
        for (Vertex<T> vertex : graph.Vertices()) {
            vertex.Parent = null;
            vertex.Distance = 0;
            vertex.Visited = false;
        }

        Queue<Vertex<T>> queue = new LinkedList<Vertex<T>>();
        queue.add(sourceVertex);

        while (queue.size() > 0) {
            Vertex<T> vertex = queue.poll();

            for (Vertex<T> neighbour : graph.GetAdjacentVertices(vertex)) {
                if (!neighbour.Visited) {
                    neighbour.Parent = vertex;
                    neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
                    neighbour.Visited = true;

                    queue.add(neighbour);
                }
            }
            vertex.Visited = true;
        }
    }

    public static <T> ArrayList<Vertex<T>> BreadthFirstSearchWithGoal(Graph<T> graph, Vertex<T> sourceVertex,
            Vertex<T> goalVertex) {
        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;
        }

        Queue<Vertex<T>> queue = new LinkedList<Vertex<T>>();
        queue.add(sourceVertex);

        while (queue.size() > 0) {
            Vertex<T> vertex = queue.poll();

            for (Vertex<T> neighbour : graph.GetAdjacentVertices(vertex)) {
                if (!neighbour.Visited) {
                    neighbour.Parent = vertex;
                    neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
                    neighbour.Visited = true;

                    if (neighbour.equals(goalVertex)) {
                        return GetPathToSource(neighbour);
                    }

                    queue.add(neighbour);
                }
            }
            vertex.Visited = true;
        }
        // No path to goal vertex exists
        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) {

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

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} v5
{Vertex} v4
Printing path from v3 to v6
{Vertex} v3
{Vertex} v1
{Vertex} v7
{Vertex} v6




Комментариев нет:

Отправить комментарий