Exemplo n.º 1
0
 public Colour ChooseColor(IColouredGraph g, Vertex v)
 {
     //some FP here //breaking away from C-style syntax
     IEnumerable<Vertex> neighbours = g.GetNeighbors(g,v);
     var validColours = _colours.Where(c => !neighbours.Any(n => _nodes.Where(_n => _n.Vertex.ID == n.ID).Any(node => (node.Vertex.Colour != null && node.Vertex.Colour.GetColour().Equals(c.GetColour()))))).Select(c => c);
     if (validColours.Any())
         return validColours.First();
     else
     {
         // should not come here. Just a fail safe precaution.
         _colours.Add(new Colour(colour + (VerticesCount + 1).ToString()));
         return _colours.Last();
     }
 }
Exemplo n.º 2
0
        private bool IsVertexPresentInNeighbours(List<Vertex> neighbours, Vertex v)
        {
            bool isPresent = false;

            foreach (var vertex in neighbours)
            {
                if (vertex.ID == v.ID)
                {
                    isPresent = true;
                    break;
                }
            }
            return isPresent;
        }
Exemplo n.º 3
0
 //Keeping the signature same for the heck of it.
 //Otherwise I wouldnt be needing the graph datatype to be passed in here.
 //May be in a pure functional programming this might make sense.
 public IColouredGraph AddEdge(IColouredGraph g, Vertex startingVertex, Vertex endingVertex)
 {
     AddVerticesAndItsNeighbours(startingVertex, endingVertex);
     _edges.Add(new Edge(startingVertex, endingVertex));
     return this;
 }
Exemplo n.º 4
0
 private int GetDegreeOfVertex(Vertex v)
 {
     int degree = 0;
     foreach (var node in _nodes)
     {
         if (node.Vertex.ID == v.ID)
         {
             degree = node.DegreeOfVertex;
             break;
         }
     }
     return degree;
 }
Exemplo n.º 5
0
        private void AddVerticesAndItsNeighbours(Vertex startingVertex, Vertex endingVertex)
        {
            int graphNodeCount = 0;
            bool startingVertexAlreadyAdded = false, endingVertexAlreadyAdded = false;

            //doing good old c-way
            for (graphNodeCount = 0; graphNodeCount < _nodes.Count; graphNodeCount++)
            {
                if (_nodes[graphNodeCount].Vertex.ID == startingVertex.ID)
                {
                    //implicitly - has atleast one neighbour

                    if (!IsVertexPresentInNeighbours(_nodes[graphNodeCount].AdjacentVertices, endingVertex))
                    {
                        _nodes[graphNodeCount].AdjacentVertices.Add(endingVertex);
                        _nodes[graphNodeCount].DegreeOfVertex++;
                    }
                    startingVertexAlreadyAdded = true;
                }

                if (_nodes[graphNodeCount].Vertex.ID == endingVertex.ID)
                {
                    //implicitly - has atleast one neighbour
                    if (!IsVertexPresentInNeighbours(_nodes[graphNodeCount].AdjacentVertices, startingVertex))
                    {
                        _nodes[graphNodeCount].AdjacentVertices.Add(startingVertex);
                        _nodes[graphNodeCount].DegreeOfVertex++;
                    }
                    endingVertexAlreadyAdded = true;

                }
            }

            //C# style of iteration
            //foreach (var graphNode in _nodes)
            //{
            //    if (graphNode.Vertex.ID == startingVertex.ID)
            //    {
            //        if (!IsVertexPresentInNeighbours(graphNode.AdjacentVertices, endingVertex))
            //        {
            //            graphNode.AdjacentVertices.Add(new Vertex(endingVertex.ID));
            //            graphNode.DegreeOfVertex++;
            //        }
            //        startingVertexAlreadyAdded = true;
            //    }

            //    if (graphNode.Vertex.ID == endingVertex.ID)
            //    {
            //        //implicitly - has atleast one neighbour
            //        if (!IsVertexPresentInNeighbours(graphNode.AdjacentVertices, startingVertex))
            //        {
            //            graphNode.AdjacentVertices.Add(new Vertex(startingVertex.ID));
            //            graphNode.DegreeOfVertex++;
            //        }
            //        endingVertexAlreadyAdded = true;
            //    }

            //}

            if (!startingVertexAlreadyAdded)
                _nodes.Add(new GraphNode(startingVertex, 1, new List<Vertex>() { new Vertex(endingVertex.ID) }));
            if (!endingVertexAlreadyAdded)
                _nodes.Add(new GraphNode(endingVertex, 1, new List<Vertex>() { new Vertex(startingVertex.ID) }));
            if (_nodes.Count > VerticesCount)
                throw new Exception("Number of vertices exceeded the intial contract as stated in the file.");
        }
Exemplo n.º 6
0
 public IEnumerable<Vertex> GetNeighbors(IColouredGraph g, Vertex v)
 {
     int graphNodeCount = 0;
     IEnumerable<Vertex> neighbours = null;
     for (graphNodeCount = 0; graphNodeCount < _nodes.Count; graphNodeCount++)
     {
         if (_nodes[graphNodeCount].Vertex.ID == v.ID)
         {
             neighbours = _nodes[graphNodeCount].AdjacentVertices;
         }
     }
     if (neighbours == null)
         neighbours = new List<Vertex>();
     return neighbours;
 }
Exemplo n.º 7
0
Arquivo: Edge.cs Projeto: 02ps719/DSA
 public Edge(Vertex startingVertex, Vertex endingVertex)
 {
     StartingVertex = startingVertex;
     EndingVertex = endingVertex;
 }
Exemplo n.º 8
0
 public GraphNode(Vertex vertex, int degree, List<Vertex> adjacentVertices)
 {
     Vertex = vertex;
     DegreeOfVertex = degree;
     AdjacentVertices = adjacentVertices;
 }