Esempio n. 1
0
 public GraphNode(T value, GraphEdge <T> edge)
 {
     this.value        = value;
     this.adjacentList = new HashSet <GraphEdge <T> >();
     if (edge != null)
     {
         this.adjacentList.Add(edge);
     }
 }
Esempio n. 2
0
        public LoadGraph(Graph <int> g, string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

            using (var reader = new StreamReader(fs, Encoding.UTF8))
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    string[]        values   = line.Split(' ');
                    GraphNode <int> nodeFrom = new GraphNode <int>(int.Parse(values[0]));
                    GraphNode <int> nodeTo   = new GraphNode <int>(int.Parse(values[1]));
                    double          distance = double.Parse(values[2]);
                    GraphEdge <int> edge     = new GraphEdge <int>(nodeFrom, nodeTo, distance);
                    g.AddVertex(nodeFrom);
                    g.AddVertex(nodeTo);
                    g.AddEdge(nodeFrom, nodeTo, distance);
                }
            }
        }
Esempio n. 3
0
        public void AddEdge(GraphNode <T> from, GraphNode <T> to, double distance)
        {
            GraphNode <T> fromNode = vertices.Single(n => n.Value.Equals(from.Value));
            GraphNode <T> toNode   = vertices.Single(n => n.Value.Equals(to.Value));


            if (fromNode == null)
            {
                this.vertices.Add(from);
                fromNode = from;
            }
            if (toNode == null)
            {
                this.vertices.Add(to);
                toNode = to;
            }

            GraphEdge <T> edge = new GraphEdge <T>(fromNode, toNode, distance);

            fromNode.AddEdge(edge);
        }