Search code examples
c#graphpriority-queueimplementation

How cna I implement a priority queue for edges in a graph?


I want to implement my own Priority Queue in C# using the naive approach. I need to use PQ for edges in a graph to implement Prims algorithm. Here is what I've done so far:

public class Edge
        {
            private int v;         // one vertex
            private int w;         // the other vertex
            private int weight;    // edge weight

            public Edge(int v0, int w0, int weight0)
              {
                  v = v0;
                  w = w0;
                  weight = weight0;
              }

           public Edge()
            {
                v = 0;
                w = 0;
                weight = 0;
            } 

            public int get_weight()
            {
                return weight;
            }

            public int either()
            {
                return v;
            }

            public int the_other(int vertex)
            {
                if (vertex == v) return w;
                else return v;
            }

            public int compareTo(Edge that)
            {
                if (this.get_weight() < that.get_weight()) return -1;
                if (this.get_weight() < that.get_weight()) return 1;
                else return 0;
            }

            public string to_str()
            {
                string s = v.ToString() + " " + weight.ToString() + " " + w.ToString() + '\n';
                return s;
            }
        }

 public class MyPriorityQueue_Edge
        {
            private List<Edge> q = new List<Edge>();
            int size;
            public MyPriorityQueue_Edge() 
            {
                size = 0;
            }

            public void insert(Edge e)
            {
                q.Add(e);
                size++;
            }

            Edge Min()
            {
                int min_weight = 1000;
                Edge min_edge = new Edge();
                foreach(Edge e in q)
                    if(e.get_weight()< min_weight)
                        min_edge = e;
                return min_edge;
            }

            public Edge delmin()
            {
                Edge e  = q.Min();
                q.Remove(e);
                size--;
                return e;
            }

          public bool is_empty()
            {
                if (size == 0) return true;
                else return false;
            }
        }

When I compile it the compiler points to this line:

Edge e  = q.Min();

And says that I did not work on the exception "System.Argument.Exception". How can I fix this?


Solution

  • From MSDN Enumerable.Min<TSource> Method (IEnumerable<TSource>)

    If type TSource implements IComparable<T>, this method uses that implementation to compare values. Otherwise, if type TSource implements IComparable, that implementation is used to compare values.

    You need to implement IComparable interface in your Edge class.

    public class Edge : IComparable

    Then add a CompareTo() method. Something like

    public int CompareTo(object obj)
    {
        if (obj is Edge)
        {
            Edge other = (Edge)obj;
            return this.compareTo(other); // you already implemented this in your Edge class
        }
        else
        {
            throw new InvalidOperationException();
        }
    }