creating weighted graph in c# code example

Example 1: creating weighted graph in c#

public class Graph
{
    public Node Root;
    public List AllNodes = new List();

    public Node CreateRoot(string name)
    {
        Root = CreateNode(name);
        return Root;
    }

    public Node CreateNode(string name)
    {
        var n = new Node(name);
        AllNodes.Add(n);
        return n;
    }

    public int?[,] CreateAdjMatrix()
    {
        // Matrix will be created here...
    }
}

Example 2: creating weighted graph in c#

public class Arc
{
    public int Weigth;
    public Node Parent;
    public Node Child;
}

Example 3: creating weighted graph in c#

public class Node
{
    public string Name;
    public List Arcs = new List();

    public Node(string name)
    {
        Name = name;
    }

    /// 
    /// Create a new arc, connecting this Node to the Nod passed in the parameter
    /// Also, it creates the inversed node in the passed node
    /// 
    public Node AddArc(Node child, int w)
    {
        Arcs.Add(new Arc
        {
            Parent = this,
            Child = child,
            Weigth = w
        });

        if (!child.Arcs.Exists(a => a.Parent == child && a.Child == this))
        {
            child.AddArc(this, w);
        }

        return this;
    }
}

Tags:

Misc Example