Пример #1
0
 public void AddVertexToList(T vertex)
 {
     if (AdjacencyList.ContainsKey(vertex))
     {
         return;
     }
     AdjacencyList[vertex] = new HashSet <Edge <T> >();
 }
Пример #2
0
 public bool HasEdge(DirectedEdge <T> edge)
 {
     if (AdjacencyList.ContainsKey(edge.FirstNode))
     {
         return(AdjacencyList[edge.FirstNode].Contains(edge.SecondNode));
     }
     return(false);
 }
Пример #3
0
 private void AddEdge(Tuple <T, T> item)
 {
     if (AdjacencyList.ContainsKey(item.Item1) && AdjacencyList.ContainsKey(item.Item2))
     {
         AdjacencyList[item.Item1].Add(item.Item2);
         AdjacencyList[item.Item2].Add(item.Item1);
     }
 }
Пример #4
0
        public void AddEdge(Tuple <T, T> edge)
        {
            if (!AdjacencyList.ContainsKey(edge.Item1))
            {
                AddVertex(edge.Item1);
            }

            AdjacencyList[edge.Item1].Add(edge.Item2);

            if (!IsDirected)
            {
                AdjacencyList[edge.Item2].Add(edge.Item1);
            }
        }
Пример #5
0
        public bool Add(Edge e)
        {
            if (!AdjacencyList.ContainsKey(e.V1))
            {
                AdjacencyList.TryAdd(e.V1, new Dictionary <char, int>());
            }
            if (!AdjacencyList.ContainsKey(e.V2))
            {
                AdjacencyList.TryAdd(e.V2, new Dictionary <char, int>());
            }

            AdjacencyList.TryGetValue(e.V1, out var e1Edges);
            AdjacencyList.TryGetValue(e.V2, out var e2Edges);

            return(e1Edges.TryAdd(e.V2, e.Weight) && e2Edges.TryAdd(e.V1, e.Weight));
        }
Пример #6
0
        // Appends a new Edge to the linked list
        public void addEdge(int startVertex, int endVertex, bool bidir = true)
        {
            if (!AdjacencyList.ContainsKey(startVertex))
            {
                AdjacencyList[startVertex] = new List <int>();
            }

            AdjacencyList[startVertex].Add(endVertex);

            if (bidir == true)
            {
                if (!AdjacencyList.ContainsKey(endVertex))
                {
                    AdjacencyList[endVertex] = new List <int>();
                }

                AdjacencyList[endVertex].Add(startVertex);
            }
        }
Пример #7
0
 // <summary>
 /// Remove edge in graph
 /// </summary>
 /// <param name="vertexFrom">From vertex</param>
 /// <param name="vertexTo">To vertex</param>
 public void RemoveEdge(T vertexFrom, T vertexTo, bool isDirected = false)
 {
     if (AdjacencyList.ContainsKey(vertexFrom) && AdjacencyList.ContainsKey(vertexTo))
     {
         var vertexToEdge   = AdjacencyList[vertexFrom].FirstOrDefault(x => x.EdgeTo.Equals(vertexTo));
         var vertexFromEdge = AdjacencyList[vertexTo].FirstOrDefault(x => x.EdgeTo.Equals(vertexFrom));
         if (vertexToEdge != null)
         {
             AdjacencyList[vertexFrom].Remove(vertexToEdge);
         }
         if (!isDirected)
         {
             if (vertexFromEdge != null)
             {
                 AdjacencyList[vertexTo].Remove(vertexFromEdge);
             }
         }
     }
 }
Пример #8
0
        /**
         * Returns true when adding new edge into the graph
         * causes a cycle. Returns false otherwise.
         *
         * The idea is to walk graph using DFS.
         */
        private bool CheckCycle(char source, char dest, ISet <char> visited)
        {
            if (!AdjacencyList.ContainsKey(dest))
            {
                return(false);
            }

            if (source == dest)
            {
                return(true);
            }

            var res = false;

            if (!AdjacencyList.TryGetValue(source, out var adjacentVertices))
            {
                return(false);
            }

            if (adjacentVertices.Count == 0)
            {
                return(false);
            }

            visited.Add(source);

            foreach (var vertex in adjacentVertices.Keys)
            {
                if (visited.Contains(vertex))
                {
                    continue;
                }

                if (res)
                {
                    return(true);
                }

                res = res || CheckCycle(vertex, dest, visited);
            }

            return(res);
        }
Пример #9
0
        public void PlaceEnemies()
        {
            if (CurrentPack == null)
            {
                return;
            }

            Order.HuntOrder.Target = this;

            Exit rn;

            if (AdjacencyList.Count > 0)
            {
                rn = (Exit)Math.Pow(2, random.Next(4));
                while (!AdjacencyList.ContainsKey(rn))
                {
                    rn = (Exit)Math.Pow(2, random.Next(4));
                }

                fleeNode = adjacencyList[rn];
            }

            else
            {
                rn       = 0;
                fleeNode = this;
            }

            switch (rn)
            {
            case Exit.Top:
                fleeLocation = new Point(TopExit, 1);
                break;

            case Exit.Bot:
                fleeLocation = new Point(BotExit, Height - 2);
                break;

            case Exit.Left:
                fleeLocation = new Point(1, LeftExit);
                break;

            case Exit.Right:
                fleeLocation = new Point(Width - 2, RightExit);
                break;

            default:
                fleeLocation = new Point(Width / 2, Height / 2);
                break;
            }

            for (int i = 0; i < CurrentPack.Size; i++)
            {
                CurrentPack[i].Location = GetRandomLocation(4);
                for (int j = 0; j < i; j++)
                {
                    if (CurrentPack[i].Location == CurrentPack[j].Location)
                    {
                        i--;
                        break;
                    }
                }
            }
        }