Ejemplo n.º 1
0
    /**************************************************************************
     *
     *  The code below is solely for testing correctness of the data type.
     *
     **************************************************************************/

    // Determines whether a digraph has an Eulerian path using necessary
    // and sufficient conditions (without computing the path itself):
    //    - indegree(v) = outdegree(v) for every vertex,
    //      except one vertex v may have outdegree(v) = indegree(v) + 1
    //      (and one vertex v may have indegree(v) = outdegree(v) + 1)
    //    - the graph is connected, when viewed as an undirected graph
    //      (ignoring isolated vertices)
    private static boolean satisfiesNecessaryAndSufficientConditions(Digraph G) {
        if (G.E() == 0) return true;

        // Condition 1: indegree(v) == outdegree(v) for every vertex,
        // except one vertex may have outdegree(v) = indegree(v) + 1
        int deficit = 0;
        for (int v = 0; v < G.V(); v++)
            if (G.outdegree(v) > G.indegree(v))
                deficit += (G.outdegree(v) - G.indegree(v));
        if (deficit > 1) return false;

        // Condition 2: graph is connected, ignoring isolated vertices
        Graph H = new Graph(G.V());
        for (int v = 0; v < G.V(); v++)
            for (int w : G.adj(v))
                H.addEdge(v, w);
        
        // check that all non-isolated vertices are connected
        int s = nonIsolatedVertex(G);
        BreadthFirstPaths bfs = new BreadthFirstPaths(H, s);
        for (int v = 0; v < G.V(); v++)
            if (H.degree(v) > 0 && !bfs.hasPathTo(v))
                return false;

        return true;
    }
Ejemplo n.º 2
0
    void Start()
    {
        Graph G = new Graph(txt);

        int s = 0;
        BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

        for (int v = 0; v < G.V(); v++)
        {
            if (bfs.hasPathTo(v))
            {
                //print("%d to %d (%d):  ", s, v, bfs.DistTo(v));
                string str = s + " to " + v + "\tdistance:" + bfs.DistTo(v) + "\t.\t";
                foreach (int x in bfs.pathTo(v))
                {
                    if (x == s)
                    {
                        str += x;
                    }
                    else
                    {
                        str += ("-" + x);
                    }
                }
                print(str);
            }

            else
            {
                print(s + " to " + v + ":  not connected\n");
            }
        }
    }
Ejemplo n.º 3
0
    /**/ public static void main(string[] strarr)
    {
        string      str         = strarr[0];
        string      str2        = strarr[1];
        string      str3        = strarr[2];
        SymbolGraph symbolGraph = new SymbolGraph(str, str2);
        Graph       g           = symbolGraph.G();

        if (!symbolGraph.contains(str3))
        {
            StdOut.println(new StringBuilder().append(str3).append(" not in database.").toString());
            return;
        }
        int i = symbolGraph.index(str3);
        BreadthFirstPaths breadthFirstPaths = new BreadthFirstPaths(g, i);

        while (!StdIn.isEmpty())
        {
            string str4 = StdIn.readLine();
            if (symbolGraph.contains(str4))
            {
                int i2 = symbolGraph.index(str4);
                if (breadthFirstPaths.hasPathTo(i2))
                {
                    Iterator iterator = breadthFirstPaths.pathTo(i2).iterator();
                    while (iterator.hasNext())
                    {
                        int i3 = ((Integer)iterator.next()).intValue();
                        StdOut.println(new StringBuilder().append("   ").append(symbolGraph.name(i3)).toString());
                    }
                }
                else
                {
                    StdOut.println("Not connected");
                }
            }
            else
            {
                StdOut.println("   Not in database.");
            }
        }
    }
    /**/ public static void main(string[] strarr)
    {
        In                i                 = new In(strarr[0]);
        Graph             graph             = new Graph(i);
        int               num               = Integer.parseInt(strarr[1]);
        BreadthFirstPaths breadthFirstPaths = new BreadthFirstPaths(graph, num);

        for (int j = 0; j < graph.V(); j++)
        {
            if (breadthFirstPaths.hasPathTo(j))
            {
                StdOut.printf("%d to %d (%d):  ", new object[]
                {
                    Integer.valueOf(num),
                    Integer.valueOf(j),
                    Integer.valueOf(breadthFirstPaths.distTo(j))
                });
                Iterator iterator = breadthFirstPaths.pathTo(j).iterator();
                while (iterator.hasNext())
                {
                    int num2 = ((Integer)iterator.next()).intValue();
                    if (num2 == num)
                    {
                        StdOut.print(num2);
                    }
                    else
                    {
                        StdOut.print(new StringBuilder().append("-").append(num2).toString());
                    }
                }
                StdOut.println();
            }
            else
            {
                StdOut.printf("%d to %d (-):  not connected\n", new object[]
                {
                    Integer.valueOf(num),
                    Integer.valueOf(j)
                });
            }
        }
    }
Ejemplo n.º 5
0
    void Start()
    {
        string      source = "JFK";
        SymbolGraph sg     = new SymbolGraph(txt);
        Graph       G      = sg.Graph();

        if (!sg.Contains(source))
        {
            print(source + " not in database.");
            return;
        }

        int s = sg.IndexOf(source);
        BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

        string sink = "DFW";

        if (sg.Contains(sink))
        {
            int t = sg.IndexOf(sink);
            if (bfs.hasPathTo(t))
            {
                foreach (int v in bfs.pathTo(t))
                {
                    print("   " + sg.NameOf(v));
                }
            }
            else
            {
                print("Not connected");
            }
        }
        else
        {
            print("   Not in database.");
        }
    }