/* Returns true if the network, src, and dest are different from previous update.
  * If not currentResultExists, returns true. */
 private bool isNeedToFindResult(GrafAdj network, string src, string dest)
 {
     if (currentResultExists)
     {
         return(!(network == currentNetwork && src == currentSrc && dest == currentDest));
     }
     return(true);
 }
Exemple #2
0
 /* Returns true if the network and user are different from previous update.
  * If not currentResultExists, returns true. */
 private bool isNeedToFindResult(GrafAdj network, string user)
 {
     if (currentResultExists)
     {
         return(!(network == currentNetwork && user == currentUser));
     }
     return(true);
 }
Exemple #3
0
        /* From IFriendRecommendation */
        public IList <Tuple <string, HashSet <string> > > getFriendRecommendation(GrafAdj network, string user)
        {
            if (isNeedToFindResult(network, user))
            {
                update(network, user);
            }

            return(new List <Tuple <string, HashSet <string> > >(currentResult));
        }
Exemple #4
0
 public Form3(Microsoft.Msagl.Drawing.Graph graph_input, GrafAdj graf_input)
 {
     InitializeComponent();
     graph = graph_input;
     //sudah digambar di form1
     //gViewer1.Graph = graph;
     graf = graf_input;
     initializeCombo();
 }
        public void bfs(GrafAdj graf, string simpulAwal, string simpulTujuan)
        {
            Dictionary <string, string> prevNode   = new Dictionary <string, string>();
            Dictionary <string, string> mapGraf    = graf.getMap();
            HashSet <string>            dikunjungi = new HashSet <string>();

            dikunjungi.Add(simpulAwal);
            Queue <string> antrian = new Queue <string>();

            antrian.Enqueue(simpulAwal);

            prevNode[simpulAwal] = null;

            while (antrian.Count > 0)
            {
                string current = antrian.Dequeue();

                if (current == simpulTujuan)
                {
                    break;
                }

                foreach (var tetangga in mapGraf[current].Split(' '))
                {
                    if (!dikunjungi.Contains(tetangga))
                    {
                        dikunjungi.Add(tetangga);
                        antrian.Enqueue(tetangga);

                        prevNode[tetangga] = current;
                    }
                }
            }

            string curr = simpulTujuan;

            hasil = new List <string>();

            while (curr != null)
            {
                hasil.Add(curr);
                string temp;
                bool   keyExists = prevNode.TryGetValue(curr, out temp);
                if (!keyExists)
                {
                    throw new KeyNotFoundException();
                }
                curr = prevNode[curr];
                derajat++;
            }

            derajat = derajat - 2; //mengexclude simpulAwal dan simpulTujuan

            hasil.Reverse();
        }
Exemple #6
0
 public Form2(Microsoft.Msagl.Drawing.Graph graph_input, GrafAdj graf_input)
 {
     InitializeComponent();
     graph = graph_input;
     //sudah digambar di form1
     gViewer1.Graph = graph;
     graf           = graf_input;
     initializeCombo();
     startButton.Enabled = false;
     string[] list_simpul = graf.getDaftarSimpul();
     foreach (var simpul in list_simpul)
     {
         graph.FindNode(simpul).Attr.FillColor = Microsoft.Msagl.Drawing.Color.White;
     }
 }
Exemple #7
0
        /* Update solution */
        private void update(GrafAdj network, string user)
        {
            currentResult = new List <Tuple <string, HashSet <string> > >();
            string userFriendsString;

            if (network.getMap().TryGetValue(user, out userFriendsString))
            {
                HashSet <string> userFriends = new HashSet <string>(userFriendsString.Split(' '));

                foreach (string friend in userFriends)
                {
                    foreach (string friendOfFriend in network.getMap().FirstOrDefault <KeyValuePair <string, string> >(edgeKvp => edgeKvp.Key == friend).Value.Split(' '))
                    {
                        if (friendOfFriend != user)
                        {
                            HashSet <string> prevHashSet = null;
                            bool             found       = false;
                            foreach (Tuple <string, HashSet <string> > prevFriend in currentResult)
                            {
                                if (prevFriend.Item1 == friendOfFriend)
                                {
                                    found       = true;
                                    prevHashSet = prevFriend.Item2;
                                    break;
                                }
                            }

                            if (found)
                            {
                                prevHashSet.Add(friend);
                            }
                            else
                            {
                                HashSet <string> friendInitialSet = new HashSet <string>();
                                friendInitialSet.Add(friend);
                                currentResult.Add(new Tuple <string, HashSet <string> >(friendOfFriend, new HashSet <string>(friendInitialSet)));
                            }
                        }
                    }
                }

                currentResult.Sort((x, y) => y.Item2.Count().CompareTo(x.Item2.Count()));
            }

            currentNetwork      = network;
            currentUser         = user;
            currentResultExists = true;
        }
        public int getDegreeConnection(GrafAdj network, string src, string dest)
        {
            if (isNeedToFindResult(network, src, dest))
            {
                update(network, src, dest);
            }

            if (currentConnectionFound)
            {
                return(currentConnection.Count - 2);
            }
            else
            {
                throw new NoConnectionException(src, dest);
            }
        }
        /* IExploreFriend implementation */
        public string[] getConnection(GrafAdj network, string src, string dest)
        {
            if (isNeedToFindResult(network, src, dest))
            {
                update(network, src, dest);
            }

            if (currentConnectionFound)
            {
                return(currentConnection.ToArray());
            }
            else
            {
                throw new NoConnectionException(src, dest);
            }
        }
        /* Update solution */
        private void update(GrafAdj network, string src, string dest)
        {
            if (src == dest)
            {
                throw new SelfConnectionException(src);
            }
            List <string> connection = new List <string>();
            string        srcAdjacentString;

            currentConnectionFound = dfs(network, src, dest, ref connection);
            currentNetwork         = network;
            currentSrc             = src;
            currentDest            = dest;
            if (currentConnectionFound)
            {
                currentConnection = new List <string>(connection);
            }
            currentResultExists = true;
        }
        /* Returns true if dest found from src in graph with DFS, solution is concatenate with route from src to dest.
         * If not found, returns false and solution does not change. */
        private bool dfs(GrafAdj graph, string src, string dest, ref List <string> solution)
        {
            if (src == dest)
            {
                solution.Add(src);
                return(true);
            }
            else if (solution.Contains(src))
            {
                return(false);
            }
            else
            {
                string srcAdjacentString;

                if (graph.getMap().TryGetValue(src, out srcAdjacentString))
                {
                    solution.Add(src);
                    bool found = false;
                    foreach (string adjacentNode in srcAdjacentString.Split(' '))
                    {
                        if (dfs(graph, adjacentNode, dest, ref solution))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        solution.Remove(src);
                    }

                    return(found);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemple #12
0
        private void button1_Click(object sender, EventArgs e)
        {
            //open from file
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                //initial dir
                InitialDirectory = @"D:\",
                Title            = "Browse Text Files",
                //check file
                CheckFileExists = true,
                CheckPathExists = true,
                //extensions
                DefaultExt       = "txt",
                Filter           = "txt files (*.txt)|*.txt",
                FilterIndex      = 2,
                RestoreDirectory = true,
                //properties
                ReadOnlyChecked = true,
                ShowReadOnly    = true
            };

            //texbox berisi path file
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }

            string file_name = openFileDialog1.FileName;

            System.Console.WriteLine(openFileDialog1.FileName);
            System.Console.WriteLine("Sebelum read");
            string[] lines = readFromFile(file_name);
            //create graf adjacency
            graf = new GrafAdj();
            graf.createMap(lines);
            System.Console.WriteLine("Simpul: " + graf.getJmlhSimpul().ToString());
            System.Console.WriteLine("Sisi: " + graf.getJmlhSisi().ToString());
            System.Console.WriteLine("Setelah read");
            //Menciptakan graph
            graph = new Microsoft.Msagl.Drawing.Graph("graph");
            gambarGraph(graph, lines);
            //enable button
            ExploreFriendButton.Enabled = true;
            RecomFriendButton.Enabled   = true;

            /*
             * //create a form
             * System.Windows.Forms.Form form = new System.Windows.Forms.Form();
             * //create a viewer object
             * Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
             * //create a graph object
             * Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");
             * //create the graph content
             * gambarGraph(graph, lines);
             * //bind the graph to the viewer
             * viewer.Graph = graph;
             * //associate the viewer with the form
             * form.SuspendLayout();
             * viewer.Dock = System.Windows.Forms.DockStyle.Fill;
             * form.Controls.Add(viewer);
             * form.ResumeLayout();
             * //show the form
             * form.ShowDialog();
             */
        }