Ejemplo n.º 1
0
 public Node(string _name)
 {
     this.name    = _name;
     this.numPred = 0;
     this.next    = null;
     this.trail   = null;
 }
Ejemplo n.º 2
0
        public void InsertEdge(string source, string destination)
        {
            Node          Pprec = SearchNode(source);      // search sourceNode
            Node          Psucc = SearchNode(destination); // search destinationNode
            SuccessorNode T;

            if (SearchEdge(source, destination) == null) // if the edge doesn't exist
            {
                T = Pprec.Trail;                         // assign T with successorNode of sourceNode

                if (T == null)                           // if the sourceNode doesn't have any neighbor
                {
                    SuccessorNode temp = new SuccessorNode(Psucc);
                    Pprec.Trail = temp;
                }
                else
                {
                    while (T.Next != null) // traversal until the last node of sourceNode's successorNode
                    {
                        T = T.Next;
                    }
                    SuccessorNode temp = new SuccessorNode(Psucc);
                    T.Next = temp;
                }
                Psucc.nPred += 1; // add neighbor counter
            }

            // for the opposite
            Pprec = SearchNode(destination); // search sourceNode
            Psucc = SearchNode(source);      // search destinationNode
            SuccessorNode Tn;

            if (SearchEdge(destination, source) == null) // if the edge doesn't exist
            {
                Tn = Pprec.Trail;                        // assign T with successorNode of sourceNode

                if (Tn == null)                          // if the sourceNode doesn't have any neighbor
                {
                    SuccessorNode temp = new SuccessorNode(Psucc);
                    Pprec.Trail = temp;
                }
                else
                {
                    while (Tn.Next != null) // traversal until the last node of sourceNode's successorNode
                    {
                        Tn = Tn.Next;
                    }
                    SuccessorNode temp = new SuccessorNode(Psucc);
                    Tn.Next = temp;
                }
                Psucc.nPred += 1; // add neighbor counter
            }
        }
Ejemplo n.º 3
0
        // Method
        public void FindMutualFriend()
        {
            // Bikin traversal graph pake bfs
            BreadthFirstSearch bfs = new BreadthFirstSearch(g, startNode);

            // shortest distance dari masing-masing account
            bfs.RunBFS();
            foreach (KeyValuePair <Node, int> NodeLevel in bfs.Level)
            {
                List <Node> mutual = new List <Node>();

                // Hanya memproses node dengan level == 2 karena ya kalo lebih gapunya mutual friend berarti
                if (NodeLevel.Value == 2)
                {
                    // 0th degree node
                    Node V = bfs.StartNode;

                    // 1st degree nodes
                    SuccessorNode TNode = V.Trail;

                    // foreach node yang ada di 1st degree connections
                    while (TNode != null)
                    {
                        // 2nd degree node
                        Node TChild = TNode.Succ;

                        // 2nd degree nodes
                        SuccessorNode ChildTrail = TChild.Trail;

                        // flag buat ngecek namanya sama atau engga, defaultnya false
                        bool status = false;

                        // foreach 2nd degree connections
                        while (!status && ChildTrail != null)
                        {
                            // ngecek namanya sama atau engga sama Nodelevel yang ada di level 2, kalo iya, tambahin ke list mutual
                            if (NodeLevel.Key.Name == ChildTrail.Succ.Name)
                            {
                                mutual.Add(TChild);
                                status = true;
                            }
                            ChildTrail = ChildTrail.Next;
                        }
                        TNode = TNode.Next;
                    }
                    result.Add(NodeLevel.Key, mutual);
                }
            }

            // sorting descending berdasarkan mutual yang paling banyak
            result = result.OrderByDescending(l => l.Value.Count).ToDictionary(l => l.Key, l => l.Value);
        }
Ejemplo n.º 4
0
        private void BFS(Node startNode)
        {
            // startNode visited, set the visited value with true
            visited[startNode] = true;
            // set the level of startNode with 0
            level[startNode] = 0;
            // add startNode into queue
            q.Enqueue(startNode);

            while (!q.IsEmpty())
            {
                // to process the node, dequeue it.
                Node V = q.Dequeue();
                // add the node into result list
                result.Add(V);

                // assign child with node V's neighbors
                SuccessorNode Child = V.Trail;
                // traversal all of V's neighbors
                while (Child != null)
                {
                    // if the succ haven't visited, visit
                    if (!visited[Child.Succ])
                    {
                        // assign visited with true
                        visited[Child.Succ] = true;
                        // assign V as the parent
                        parentNode[Child.Succ] = V;
                        // assign the level with the parent's level + 1
                        level[Child.Succ] = level[V] + 1;
                        // enqueue
                        q.Enqueue(Child.Succ);
                    }
                    Child = Child.Next;
                }
            }
        }
Ejemplo n.º 5
0
        private void DFS(Node startNode)
        {
            visited[startNode]     = 1; // startNode visited, set the visited value to 1
            Interval[startNode][0] = t; // time when the startNode visited for the first time

            result.Add(startNode);      // add startNode to result list
            t++;

            SuccessorNode TrailNode = startNode.Trail;      // set trailNode with startNode's successorNode

            while (TrailNode != null)                       // traversal until all of the startNode's visited
            {
                if (visited[TrailNode.Succ] == 0)           // if the succ of trailNode haven't visited
                {
                    parentNode[TrailNode.Succ] = startNode; // set the trailNode's succ with startNode
                    DFS(TrailNode.Succ);                    // Recursively call DFS until reaching the leaf
                }
                TrailNode = TrailNode.Next;
            }

            visited[startNode]     = 2; // Backtrack
            interval[startNode][1] = t; // time when the startNode visited for the second time
            t++;
        }