Пример #1
0
		private static PathList FindPaths(IGraph graph,
			INode from, INode to, bool shortestOnly)
		{
			PathList result = new PathList();
			Path path = null;

			if (from == to && shortestOnly)
			{
				// Return a collection with a single path consisting of a single node.
				path = new Path();
				path.Add(from);
				result.Add(path);

				return result;
			}

			// Perform the search
			PathList tempCol = new PathList();
			Path tempPath;

			// Create the first path, constisting only of the first node
			tempCol.Add(tempPath = new Path());
			tempPath.Add(from);

			bool pathFound = false;
			while (true)
			{
				int size = tempCol.Count;
				if (size == 0)
					break;
				if (pathFound && shortestOnly)
					break;

				// For all paths - get their next nodes
				for (int i = 0; i < size; i++)
				{
					tempPath = tempCol[0];
					tempCol.RemoveAt(0);

					// Get the last node for this path and find its successors
					INode lastNode = tempPath.Nodes[tempPath.Nodes.Count - 1];
					LinkCollection links = lastNode.OutLinks;
					foreach (ILink link in links)
					{
						INode nextNode;

						// Get the next node in the path
						nextNode = graph.Directed ?
							link.Destination : link.GetOppositeNode(lastNode);

						// Check if the path target is reached
						if (nextNode.Equals(to))
						{
							// We've reached the end
							Path newPath = new Path(tempPath);
							newPath.Add(link, nextNode);
							result.Add(newPath);
							pathFound = true;
							continue;
						}

						// The node does not belong to the path -> add it
						if (!tempPath.Contains(nextNode))
						{
							Path newPath = new Path(tempPath);
							newPath.Add(link, nextNode);
							tempCol.Add(newPath);
						}
					}
				}
			}

			return result;
		}
Пример #2
0
        private static PathList FindPaths(IGraph graph,
                                          INode from, INode to, bool shortestOnly)
        {
            PathList result = new PathList();
            Path     path   = null;

            if (from == to && shortestOnly)
            {
                // Return a collection with a single path consisting of a single node.
                path = new Path();
                path.Add(from);
                result.Add(path);

                return(result);
            }

            // Perform the search
            PathList tempCol = new PathList();
            Path     tempPath;

            // Create the first path, constisting only of the first node
            tempCol.Add(tempPath = new Path());
            tempPath.Add(from);

            bool pathFound = false;

            while (true)
            {
                int size = tempCol.Count;
                if (size == 0)
                {
                    break;
                }
                if (pathFound && shortestOnly)
                {
                    break;
                }

                // For all paths - get their next nodes
                for (int i = 0; i < size; i++)
                {
                    tempPath = tempCol[0];
                    tempCol.RemoveAt(0);

                    // Get the last node for this path and find its successors
                    INode          lastNode = tempPath.Nodes[tempPath.Nodes.Count - 1];
                    LinkCollection links    = lastNode.OutLinks;
                    foreach (ILink link in links)
                    {
                        INode nextNode;

                        // Get the next node in the path
                        nextNode = graph.Directed ?
                                   link.Destination : link.GetOppositeNode(lastNode);

                        // Check if the path target is reached
                        if (nextNode.Equals(to))
                        {
                            // We've reached the end
                            Path newPath = new Path(tempPath);
                            newPath.Add(link, nextNode);
                            result.Add(newPath);
                            pathFound = true;
                            continue;
                        }

                        // The node does not belong to the path -> add it
                        if (!tempPath.Contains(nextNode))
                        {
                            Path newPath = new Path(tempPath);
                            newPath.Add(link, nextNode);
                            tempCol.Add(newPath);
                        }
                    }
                }
            }

            return(result);
        }