Ejemplo n.º 1
0
        public void DiGraph_Smoke_Test()
        {
            var graph = new DiGraph <int>();

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);
            graph.AddVertex(5);

            graph.AddEdge(1, 2);
            Assert.IsTrue(graph.HasEdge(1, 2));
            Assert.IsFalse(graph.HasEdge(2, 1));

            graph.AddEdge(2, 3);
            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(4, 1);
            graph.AddEdge(3, 5);

            //IEnumerable test using linq
            Assert.AreEqual(graph.VerticesCount, graph.Count());

            Assert.AreEqual(2, graph.OutEdges(4).Count());
            Assert.AreEqual(2, graph.InEdges(5).Count());

            Assert.AreEqual(5, graph.VerticesCount);

            Assert.IsTrue(graph.HasEdge(1, 2));

            graph.RemoveEdge(1, 2);

            Assert.IsFalse(graph.HasEdge(1, 2));

            graph.RemoveEdge(2, 3);
            graph.RemoveEdge(3, 4);
            graph.RemoveEdge(4, 5);
            graph.RemoveEdge(4, 1);

            Assert.IsTrue(graph.HasEdge(3, 5));
            graph.RemoveEdge(3, 5);
            Assert.IsFalse(graph.HasEdge(3, 5));

            graph.RemoveVertex(1);
            graph.RemoveVertex(2);
            graph.RemoveVertex(3);
            graph.RemoveVertex(4);

            graph.AddEdge(5, 5);
            graph.RemoveEdge(5, 5);
            graph.RemoveVertex(5);

            Assert.AreEqual(0, graph.VerticesCount);

            //IEnumerable test using linq
            Assert.AreEqual(graph.VerticesCount, graph.Count());
        }
Ejemplo n.º 2
0
    public int FindJudge(int N, int[][] trust)
    {
        DiGraph di = new DiGraph(N);

        for (int i = 0; i < trust.Length; i++)
        {
            di.AddEdge(trust[i][0] - 1, trust[i][1] - 1);
        }

        int ansCandidate = -1;

        for (int i = 0; i < di.Count(); i++)
        {
            if (di.OutDeg(i) == 0)
            {
                if (ansCandidate == -1)
                {
                    ansCandidate = i;
                }
                else
                {
                    return(-1);
                }
            }
        }

        if (ansCandidate == -1)
        {
            return(-1);
        }

        for (int i = 0; i < di.Count(); i++)
        {
            bool isTrust = false;
            if (i != ansCandidate)
            {
                foreach (var j in di.Adj(i))
                {
                    if (j == ansCandidate)
                    {
                        isTrust = true;
                        break;
                    }
                }

                if (!isTrust)
                {
                    return(-1);
                }
            }
        }

        return(ansCandidate + 1);
    }