Example #1
0
 public Boolean addVertex(Vertex v)
 {
     // if v already exists in graph, return false
     if (vertices.ContainsKey(v.getName()))
     {
         return(false);
     }
     // otherwise try adding it
     vertices.Add(v.getName(), v);
     return(true);
 }
Example #2
0
    public List <Vertex> computePaths(List <Vertex> graph, Vertex sourceVertex)
    {
        List <Vertex> list      = graph;
        List <Vertex> prioQueue = new List <Vertex>();

        foreach (Vertex v in list)
        {
            if (v.getName() != sourceVertex.getName())
            {
                prioQueue.Add(v);
            }
        }
        sourceVertex = graph.Find(v => v.getName().Equals(sourceVertex.getName()));
        sourceVertex.setDistance(0);

        prioQueue.Add(sourceVertex);

        while (prioQueue.Count != 0)
        {
            bubbleSort(prioQueue);
            Vertex actualVertex = prioQueue[0];
            string x            = actualVertex.getName();
            prioQueue.Remove(actualVertex);

            int len = actualVertex.getEdgeList().Count;
            foreach (Edge edge in actualVertex.getEdgeList())
            {
                string vName = edge.getEndV().getName();
                Vertex v     = null;
                foreach (Vertex node in list)
                {
                    if (vName == node.getName())
                    {
                        v = node;
                        break;
                    }
                }

                float newDistance = actualVertex.getDistance() + edge.getWeight();

                if (newDistance < v.getDistance())
                {
                    v.setDistance(newDistance);
                    v.setLastVertex(actualVertex);
                    prioQueue.Add(v);
                }
            }
        }
        return(list);
    }
Example #3
0
    public List <Vertex> getShortestPath(Vertex targetVertex, List <Vertex> list)
    {
        List <Vertex> shortestPathToTarget = new List <Vertex>();

        Vertex v = list.Find(i => i.getName().Equals(targetVertex.getName()));

        for (Vertex vertex = v; vertex != null; vertex = vertex.getLastVertex())
        {
            shortestPathToTarget.Add(vertex);
        }

        shortestPathToTarget.Reverse();
        return(shortestPathToTarget);
    }
Example #4
0
            ///// <summary>
            ///// Get all the children of this Vertex
            ///// </summary>
            ///// <returns></returns>
            //public HashSet<Vertex> getChildren()
            //{
            //    HashSet<Vertex> children = new HashSet<Vertex>();
            //    populateChildren(this, children);
            //    return children;
            //}

            //private void populateChildren(Vertex parent, HashSet<Vertex> children)
            //{
            //    if (parent.adj != null)
            //    {
            //        foreach (Edge e in parent.adj)
            //        {
            //            children.Add(e.getOtherVertex());
            //            populateChildren(e.getOtherVertex(), children);
            //        }
            //    }
            //}

            /// <summary>
            /// Compares this vertex with another for quality based on their name fields
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public bool equals(Object obj)
            {
                if (!(obj is Vertex))
                {
                    return(false);
                }

                Vertex other = (Vertex)obj;

                if (this.name.CompareTo(other.getName()) == 0)
                {
                    return(true);
                }

                return(false);
            }
Example #5
0
		public Edge findEdge(Vertex vet1, Vertex vet2) {
			if (!vet1.Equals(vet2)) {
				if (directed) {
					foreach (Edge e in edges)
						if (((e.getStart().getName().Equals(vet1.getName())) &&
							(e.getEnd().getName().Equals(vet2.getName()))))
							return e;
				} else {
					foreach (Edge e in edges)
						if (((e.getStart().getName().Equals(vet1.getName())) &&
							(e.getEnd().getName().Equals(vet2.getName()))) ||
							((e.getStart().getName().Equals(vet2.getName())) &&
								(e.getEnd().getName().Equals(vet1.getName()))))
							return e;
				}
			}
			return null;

		}
Example #6
0
        /// <summary>
        /// Performs Dijkstra's routine on a weighted graph to determine the cheapest path from start vertex to a goal vertex.
        /// </summary>
        /// <param name="graph">
        /// The graph object to be traversed
        /// </param>
        /// <param name="startName">
        /// Name of the starting vertex in the path
        /// </param>
        /// <param name="goalName">
        /// Name of the ending vertex in the path
        /// </param>
        /// <returns>
        /// String List of the vertices that make up the cheapest path from the starting vertex (inclusive) to the
        /// ending vertex (inclusive) based on weight associated with the edges between the graphs vertices
        /// </returns>
        public static List <String> shortestPath(Graph graph, String startName, String goalName)
        {
            Queue <Vertex> queue = new Queue <Vertex>();
            List <String>  path  = new List <String>();

            Vertex start;

            graph.getVertices().TryGetValue(startName, out start);
            Vertex goal;

            graph.getVertices().TryGetValue(goalName, out goal);

            // if start and goal are the same, return one
            if (start.equals(goal))
            {
                path.Add(startName);
                return(path);
            }

            // Initialize all nodes' cost to infinity
            foreach (Vertex v in graph.getVertices().Values)
            {
                v.setCostFromStart(int.MaxValue);
                v.setVisited(false);
                v.setCameFrom(null);
            }

            // Enqueue the start
            queue.Enqueue(start);
            start.setCostFromStart(0);

            while (queue.Count != 0)
            {
                Vertex curr = queue.Dequeue();

                if (curr.equals(goal))
                {
                    break;
                }
                curr.setVisited(true);

                //get all of current's neighbors
                foreach (Edge e in curr.getEdges())
                {
                    Vertex neighbor = e.getOtherVertex();
                    if (neighbor.getVisited() == false)
                    {
                        LinkedList <Edge> edges = curr.getEdges();

                        foreach (Edge E in edges)
                        {
                            if (E.getOtherVertex().equals((neighbor)))
                            {
                                // if neighbor's cost from start is higher than current cost + edge weight,
                                // enqueue and neighbor becomes current
                                if (neighbor.getCostFromStart() > (curr.getCostFromStart() + E.getWeight()))
                                {
                                    queue.Enqueue(neighbor);
                                    neighbor.setCameFrom(curr);
                                    neighbor.setCostFromStart(curr.getCostFromStart() + E.getWeight());
                                }
                            }
                        }
                    }
                }
            }

            if (goal.getCameFrom() == null)
            {
                return(path);
            }

            //get the data from the cameFrom and add it to the list to be returned
            for (Vertex v = goal; v != null; v = v.getCameFrom())
            {
                path.Add(v.getName());
            }

            //If no path was found, return an empty list
            if (!path.Contains(goalName) && !path.Contains(startName))
            {
                path.Clear();
                return(path);
            }
            else
            {
                return(path);
            }
        }