Example #1
0
        public virtual void AddEdge(Vertex <T> left, Vertex <T> right, TEdge item)
        {
            left.AddNeighbor(right);
            Edge <T, TEdge> edge = new(left, right, item);

            Edges.Add(edge);
        }
Example #2
0
        public static void Main()
        {
            Random rnd = new Random();
            List <Vertex <int> > graph = new List <Vertex <int> >();

            for (int i = 0; i < 5; i++)             // Create vertices
            {
                graph.Add(new Vertex <int>(i));
            }

            for (int i = 0; i < 5; i++)               // Add edges
            {
                Vertex <int> v = graph[i];
                for (int j = 0; j < 5; j++)
                {
                    v.AddNeighbor(graph[rnd.Next(5)]);
                }
            }

            int index = rnd.Next(5);             // Add some more edges

            for (int i = 0; i < 5; i++)
            {
                graph[i].AddNeighbor(graph[index]);
            }

            listerAretes(graph);
        }
Example #3
0
    public void AddEdge(Vector3 v1, Vector3 v2)
    {
        Vertex ve1 = Vertices[v1];
        Vertex ve2 = Vertices[v2];

        ve1.AddNeighbor(ve2);
        ve2.AddNeighbor(ve1);
    }
Example #4
0
    // Adds an edge without weights if Weighted layer is not present
    public EdgeIfc AddEdge(Vertex start,  Vertex end) {
        Edge theEdge = new  Edge();
        theEdge.EdgeConstructor( start, end );
        edges.Add( theEdge );
        start.AddNeighbor( new  Neighbor( end, theEdge ) );
        end.AddNeighbor( new  Neighbor( start, theEdge ) );

        return theEdge;
    }
Example #5
0
    // Adds an edge without weights if Weighted layer is not present
    public EdgeIfc AddEdge(Vertex start, Vertex end)
    {
        Edge theEdge = new  Edge();

        theEdge.EdgeConstructor(start, end);
        edges.Add(theEdge);
        start.AddNeighbor(new  Neighbor(end, theEdge));
        end.AddNeighbor(new  Neighbor(start, theEdge));

        return(theEdge);
    }
Example #6
0
 public Edge(Vertex <T> start, Vertex <T> end, int cost)
 {
     this.start = start;
     this.end   = end;
     this.cost  = cost;
     if (start.Neighbors.Find(v => ReferenceEquals(v.Tile, end.Tile)) == null)
     {
         start.AddNeighbor(end);
     }
     if (end.Neighbors.Find(v => ReferenceEquals(v.Tile, start.Tile)) == null)
     {
         end.AddNeighbor(start);
     }
 }
Example #7
0
        public static void Main()
        {
            Random rnd = new Random();
            List <Vertex <int> > graph = new List <Vertex <int> >();

            for (int i = 0; i < 5; i++)             // Create vertices
            {
                graph.Add(new Vertex <int>(i));
            }

            for (int i = 0; i < 5; i++)               // Add edges
            {
                Vertex <int> v = graph[i];
                for (int j = 0; j < 5; j++)
                {
                    v.AddNeighbor(graph[rnd.Next(5)]);
                }
            }
            int index = rnd.Next(5);             // Add some more edges

            for (int i = 0; i < 5; i++)
            {
                graph[i].AddNeighbor(graph[index]);
            }

            Console.WriteLine(" --- --- --- --- Part1 --- --- --- --- ");

            List <Tuple <int, int> > q1 = GetEdges(graph);

            foreach (Tuple <int, int> i in q1)
            {
                Console.WriteLine("{0} -> {1}", i.Item1, i.Item2);
            }

            Console.WriteLine(" --- --- --- --- Part2 --- --- --- --- ");

            Q2 q2 = new Q2();
            List <Tuple <int, int> > q2res = q2.MapCollect(graph);

            for (int i = 0; i < q2res.Count; i++)
            {
                Console.WriteLine("Data : {0} - nb occurence : {1}", q2res[i].Item1, q2res[i].Item2);
            }
        }
Example #8
0
 public void AddEdge(Vertex firstVertex, Vertex secondVertex)
 {
     firstVertex.AddNeighbor(secondVertex);
     secondVertex.AddNeighbor(firstVertex);
 }
Example #9
0
 private void AddEdge(Vertex firstVertex, Vertex secondVertex)
 {
     firstVertex.AddNeighbor(secondVertex);
     secondVertex.AddNeighbor(firstVertex);
 }
Example #10
0
    private void Deserialize(string path)
    {
        int intSize   = 4;
        int floatSize = 4;

        using (var stream = File.Open(path, FileMode.Open))
        {
            Dictionary <Vector3, Vertex> dic = new Dictionary <Vector3, Vertex>();
            byte[] buf = new byte[16];
            stream.Read(buf, 0, intSize);
            int         dicSize = BitConverter.ToInt32(buf, 0);
            Vector3[]   v3      = new Vector3[dicSize];
            Vertex[]    ve      = new Vertex[dicSize];
            Vector3[][] neigh   = new Vector3[dicSize][];
            for (int i = 0; i < dicSize; i++)
            {
                stream.Read(buf, 0, floatSize);
                float x = BitConverter.ToSingle(buf, 0);
                stream.Read(buf, 0, floatSize);
                float y = BitConverter.ToSingle(buf, 0);
                stream.Read(buf, 0, floatSize);
                float z = BitConverter.ToSingle(buf, 0);
                v3[i]      = new Vector3(x, y, z);
                ve[i]      = new Vertex(v3[i], null);
                minLimit.x = Mathf.Min(minLimit.x, x);
                maxLimit.x = Mathf.Max(maxLimit.x, x);
                minLimit.y = Mathf.Min(minLimit.y, y);
                maxLimit.y = Mathf.Max(maxLimit.y, y);
                minLimit.z = Mathf.Min(minLimit.z, z);
                maxLimit.z = Mathf.Max(maxLimit.z, z);
                stream.Read(buf, 0, intSize);
                int neighSize = BitConverter.ToInt32(buf, 0);
                neigh[i] = new Vector3[neighSize];
                dic.Add(v3[i], ve[i]);
                for (int j = 0; j < neighSize; j++)
                {
                    stream.Read(buf, 0, floatSize);
                    float vx = BitConverter.ToSingle(buf, 0);
                    stream.Read(buf, 0, floatSize);
                    float vy = BitConverter.ToSingle(buf, 0);
                    stream.Read(buf, 0, floatSize);
                    float vz = BitConverter.ToSingle(buf, 0);
                    neigh[i][j] = new Vector3(vx, vy, vz);
                }
            }

            for (int i = 0; i < dicSize; i++)
            {
                Vertex v = ve[i];
                for (int j = 0; j < neigh[i].Length; j++)
                {
                    v.AddNeighbor(dic[neigh[i][j]]);
                }
            }
            graph = new Graph(dic);
            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;
            Debug.Log(String.Format("Deserialize graph {0}ms", ts.Milliseconds, DebugMode));
            stream.Close();
        }
    }
Example #11
0
 protected void AddEdge(Vertex firstVertex, Vertex secondVertex)
 {
     firstVertex.AddNeighbor(secondVertex);
     secondVertex.AddNeighbor(firstVertex);
 }
        public Vertex BuildGraph(string site, Vertex parent, out float sim)
        {
            // Decrement count because we just went down a level
            --levelCount;
            sim = 0;
            if (siteCount >= MAXSITES)
            {
                return(null);
            }
            try
            {
                // Take one site and a parent vertex
                // Get data and list of sites
                List <string> parsedData;
                List <string> sites;
                GetWebDataAgility(site, out parsedData, out sites);
                // Fill a new HTable (frequency table)
                HTable vTable = new HTable();
                vTable.ID   = ++TableNumber;
                vTable.URL  = site;
                vTable.Name = site.Substring(30);
                if (parsedData != null)
                {
                    for (int w = 0; w < parsedData.Count; ++w)
                    {
                        vTable.Put(parsedData[w], 1);
                    }
                }
                // Write HTable to file
                vTable.SaveTable(vTable.ID);
                // Create a vertex for this site with the same ID as HTable
                Vertex v = new Vertex(vTable.ID, vTable.URL);

                // Create an edge connecting this vertex and parent vertex
                if (parent != null)
                {
                    // Calc similiarty to parent
                    HTable          parentTable = LoadTable(parent.ID);
                    List <object>[] vector      = WebCompareModel.BuildVector(vTable, parentTable);
                    //// Calcualte similarity, 1 minus for shortest path calculation, lower numbers are more similar
                    sim = 1 - (float)WebCompareModel.CosineSimilarity(vector);
                    //Create edge to parent
                    Edge e1 = new Edge(parent.ID, v.ID, sim, ++EdgeNumber);
                    Edge e2 = new Edge(v.ID, parent.ID, sim, ++EdgeNumber);
                    //mainGraph.AddEdge(e1);   // Add edge to Graph list
                    //mainGraph.SaveEdge(e1);  // Write edge to disk
                    //mainGraph.AddEdge(e2);   // Add edge to Graph list
                    //mainGraph.SaveEdge(e2);  // Write edge to disk
                    // Add eachother as neighbors
                    parent.AddNeighbor(e1);
                    v.AddNeighbor(e2);
                    // Update parent vertex
                    mainGraph.AddVertex(parent);
                    mainGraph.SaveVertex(parent);
                }
                // Update/Add to graph
                mainGraph.AddVertex(v);
                mainGraph.SaveVertex(v);
                ++siteCount;

                // Add list of sites to this vertex
                //// Forach- add, recursively call this method
                foreach (var s in sites)
                {
                    // Check for nulls, Don't compare to itself
                    if (s == null || s == site)
                    {
                        continue;
                    }

                    // Don't get more sites if site tree has been built already
                    Vertex v2 = mainGraph.HasVertex(s);
                    if (v2 != null)
                    {
                        LoadStatus += ".";
                        // Calc similiarty to parent
                        HTable v2Table = LoadTable(v2.ID);
                        sim = 0;   // clear
                        if (v2Table != null)
                        {
                            List <object>[] vector = WebCompareModel.BuildVector(vTable, v2Table);
                            //// Calcualte similarity, 1 minus for shortest path calculation, lower numbers are more similar
                            sim = 1 - (float)WebCompareModel.CosineSimilarity(vector);
                            //Create edge to parent
                            Edge e = new Edge(v.ID, v2.ID, (float)sim, ++EdgeNumber);
                            //mainGraph.AddEdge(e);   // Add edge to Graph list
                            //mainGraph.SaveEdge(e);  // Write edge to disk

                            // Add eachother as neighbors
                            v.AddNeighbor(e);
                            v2.AddNeighbor(new Edge(v2.ID, v.ID, (float)sim, ++EdgeNumber));
                            // Update/Add to graph
                            mainGraph.AddVertex(v);
                            mainGraph.AddVertex(v2);
                        }

                        mainGraph.SaveVertex(v2);
                    }
                    else
                    {
                        float  simout = 0;
                        Vertex neighb = null;

                        // Don't build another graph off this node if the level count hits 0
                        if (levelCount > 0)
                        {
                            neighb = BuildGraph(s, v, out simout);
                            // Increment levelcount because we just came back up a level
                            ++levelCount;
                        }
                        else
                        {
                            return(v);
                        }

                        if (neighb != null)
                        {
                            Edge newEdge = new Edge(v.ID, neighb.ID, simout, EdgeNumber++);
                            v.AddNeighbor(newEdge);
                            //mainGraph.SaveEdge(newEdge);
                        }
                    }
                }
                // Update Vertex to graph and persist
                if (v != null)
                {
                    mainGraph.AddVertex(v);
                    mainGraph.SaveVertex(v);
                }

                return(v);
            }
            catch (Exception exc) { Console.WriteLine("Error building graph: " + exc); }
            sim = 0;
            return(null);
        }
Example #13
0
 public void AddEdge(Vertex startVertex, Vertex endVertex)
 => startVertex.AddNeighbor(endVertex);