public static HashSet <AllPurpuseEntity> VisitedNodes(Graph graph, AllPurpuseEntity start) { var visited = new HashSet <AllPurpuseEntity>(); if (!graph.AdjacencyList.ContainsKey(start)) { return(visited); } var queue = new Queue <AllPurpuseEntity>(); queue.Enqueue(start); while (queue.Count > 0) { var vertex = queue.Dequeue(); if (visited.Contains(vertex)) { continue; } visited.Add(vertex); foreach (var neighbor in graph.AdjacencyList[vertex]) { if (!visited.Contains(neighbor)) { queue.Enqueue(neighbor); } } } return(visited); }
/// <summary> /// Pronadji oba node-a /// </summary> /// <param name="firstId"></param> /// <param name="secondId"></param> /// <returns> /// Item1 : Node with firstId /// Item2 : Node with secondId /// </returns> private Tuple <AllPurpuseEntity, AllPurpuseEntity> GetFSEnds(long firstId, long secondId) { AllPurpuseEntity first = null; bool foundedFist = false; AllPurpuseEntity second = null; bool foundedSecond = false; for (int i = 0; i < DataContainers.Containers.AllPurpuseEntities.Count; ++i) { if (foundedFist == true && foundedSecond == true) { break; } if (DataContainers.Containers.AllPurpuseEntities.ElementAt(i).Id == firstId) { first = DataContainers.Containers.AllPurpuseEntities.ElementAt(i); foundedFist = true; continue; } if (DataContainers.Containers.AllPurpuseEntities.ElementAt(i).Id == secondId) { second = DataContainers.Containers.AllPurpuseEntities.ElementAt(i); foundedSecond = true; } } return(new Tuple <AllPurpuseEntity, AllPurpuseEntity>(first, second)); }
private static Rectangle CreateEllipse(AllPurpuseEntity allPurpuseEntity) => new Rectangle { Width = MainWindow.CanvasData().Item3, Height = MainWindow.CanvasData().Item3, Fill = System.Windows.Media.Brushes.Black, Stroke = System.Windows.Media.Brushes.Black, StrokeThickness = 1 };
public static Func <AllPurpuseEntity, IEnumerable <AllPurpuseEntity> > ShortestPathFunction(Graph graph, AllPurpuseEntity start) { // recnik prethodnih, kljuc je child var previous = new Dictionary <AllPurpuseEntity, AllPurpuseEntity>(); var queue = new Queue <AllPurpuseEntity>(); queue.Enqueue(start); while (queue.Count > 0) { var vertex = queue.Dequeue(); foreach (var neighbor in graph.AdjacencyList[vertex]) { if (previous.ContainsKey(neighbor)) { continue; } previous[neighbor] = vertex; queue.Enqueue(neighbor); } } Func <AllPurpuseEntity, IEnumerable <AllPurpuseEntity> > shortestPath = v => { var path = new List <AllPurpuseEntity> { }; // node koji se posmatra var current = v; while (!current.Equals(start)) // ide kroz recnik sve kod ne naidje na samog sebe { path.Add(current); try { current = previous[current]; } catch { return(null); } } ; path.Add(start); path.Reverse(); return(path); }; return(shortestPath); }
/// <summary> /// Prolazak kroz graf /// </summary> /// <param name="graph"></param> /// <param name="start"></param> /// <returns> /// Item1 : recnik povezanih cvorova /// Item2 : hashset putanje /// </returns> public static Tuple <Dictionary <AllPurpuseEntity, AllPurpuseEntity>, HashSet <AllPurpuseEntity> > BFS(Graph graph, AllPurpuseEntity start) { var connectedNodes = new Dictionary <AllPurpuseEntity, AllPurpuseEntity>(); var visited = new HashSet <AllPurpuseEntity>(); if (!graph.AdjacencyList.ContainsKey(start)) { return(new Tuple <Dictionary <AllPurpuseEntity, AllPurpuseEntity>, HashSet <AllPurpuseEntity> >(connectedNodes, visited)); } var queue = new Queue <AllPurpuseEntity>(); queue.Enqueue(start); while (queue.Count > 0) { var vertex = queue.Dequeue(); if (visited.Contains(vertex)) { continue; } visited.Add(vertex); foreach (var neighbor in graph.AdjacencyList[vertex]) { if (connectedNodes.ContainsKey(neighbor)) { continue; } connectedNodes[neighbor] = vertex; if (!visited.Contains(neighbor)) { queue.Enqueue(neighbor); } } } return(new Tuple <Dictionary <AllPurpuseEntity, AllPurpuseEntity>, HashSet <AllPurpuseEntity> >(connectedNodes, visited)); }
public static List <AllPurpuseEntity> Path(Dictionary <AllPurpuseEntity, AllPurpuseEntity> connectedNodes, AllPurpuseEntity start, AllPurpuseEntity v) { var path = new List <AllPurpuseEntity> { }; // node koji se posmatra var current = v; while (!current.Equals(start)) // ide kroz recnik sve kod ne naidje na samog sebe { path.Add(current); try { current = connectedNodes[current]; } catch { return(null); } } ; path.Add(start); path.Reverse(); return(path); }
/// <summary> /// Daje listu koordita izmedju 2 dva cvora /// </summary> /// <param name="startNode">prvi cvor</param> /// <param name="endNode">drugi cvor</param> /// <returns></returns> private static HashSet <AllPurpuseEntity> SetCoordsForLine(AllPurpuseEntity startNode, AllPurpuseEntity endNode) { HashSet <AllPurpuseEntity> retVal = new HashSet <AllPurpuseEntity>(); // ako su u istoj koloni if (startNode.X == endNode.X) { if (startNode.Y > endNode.Y) { for (int y = startNode.Y; y > endNode.Y; y--) { if (CheckCoords(startNode.X, y)) { retVal.Add(new AllPurpuseEntity(startNode.X, y)); } } } else if (startNode.Y < endNode.Y) { for (int y = 0; y < endNode.Y; y++) { if (CheckCoords(startNode.X, y)) { retVal.Add(new AllPurpuseEntity(startNode.X, y)); } } } } // ako su u istom redu if (startNode.Y == endNode.Y) { if (startNode.X > endNode.X) { for (int x = startNode.X; x > endNode.X; x--) { if (CheckCoords(x, startNode.Y)) { retVal.Add(new AllPurpuseEntity(x, startNode.Y)); } } } else if (startNode.X < endNode.X) { for (int x = startNode.X; x < endNode.X; x++) { if (CheckCoords(x, startNode.Y)) { retVal.Add(new AllPurpuseEntity(x, startNode.Y)); } } } } // I kvadrant if (startNode.X < endNode.X && startNode.Y > endNode.Y) { int x = startNode.X; for (; x < endNode.X; x++) { if (CheckCoords(x, startNode.Y)) { retVal.Add(new AllPurpuseEntity(x, startNode.Y)); } } for (int y = startNode.Y; y > endNode.Y; y--) { if (CheckCoords(x, y)) { retVal.Add(new AllPurpuseEntity(x, y)); } } } // II kvadrant if (startNode.X > endNode.X && startNode.Y > endNode.Y) { int x = startNode.X; for (; x > endNode.X; x--) { if (CheckCoords(x, startNode.Y)) { retVal.Add(new AllPurpuseEntity(x, startNode.Y)); } } for (int y = startNode.Y; y > endNode.Y; y--) { if (CheckCoords(x, y)) { retVal.Add(new AllPurpuseEntity(x, y)); } } } // III kvadrant if (startNode.X > endNode.X && startNode.Y < endNode.Y) { int x = startNode.X; for (; x > endNode.X; x--) { if (CheckCoords(x, startNode.Y)) { retVal.Add(new AllPurpuseEntity(x, startNode.Y)); } } for (int y = startNode.Y; y < endNode.Y; y++) { if (CheckCoords(x, y)) { retVal.Add(new AllPurpuseEntity(x, y)); } } } // IV kvadrant if (startNode.X < endNode.X && startNode.Y < endNode.Y) { int x = startNode.X; for (; x < endNode.X; x++) { if (CheckCoords(x, startNode.Y)) { retVal.Add(new AllPurpuseEntity(x, startNode.Y)); } } for (int y = startNode.Y; y < endNode.Y; y++) { if (CheckCoords(x, y)) { retVal.Add(new AllPurpuseEntity(x, y)); } } } return(retVal); }
private static AllPurpuseEntity SetCoords(AllPurpuseEntity allPurpuseEntity) { ToLatLon(allPurpuseEntity.Entity.X, allPurpuseEntity.Entity.Y, 34, out double latitude, out double longitude); double XSpot = ((latitude - closestX) / (farthestX - closestX)) * (MainWindow.CanvasData().Item2 - 1); double YSpot = ((longitude - closestY) / (farthestY - closestY)) * (MainWindow.CanvasData().Item1 - 1); XSpot = XSpot - XSpot % MainWindow.CanvasData().Item3; YSpot = YSpot - YSpot % MainWindow.CanvasData().Item3; Coord coord = new Coord((int)XSpot, (int)YSpot); if (!allCords.Any(i => i.X == coord.X && i.Y == coord.Y)) { allCords.Add(coord); allPurpuseEntity.X = (int)XSpot; allPurpuseEntity.Y = (int)YSpot; } else { bool foundedFreeSpot = false; bool end = false; int levelElements = 3; int level = 1; while (foundedFreeSpot == false && end == false) { int newX = 0; if (XSpot != 0) { newX = (int)XSpot - level; } int newY = 0; if (YSpot != 0) { newY = (int)YSpot - level; } for (int i = 0; i < levelElements; i++) { coord.X = newX + i; coord.Y = newY; if (!allCords.Any(t => t.X == coord.X && t.Y == coord.Y)) { allCords.Add(coord); foundedFreeSpot = true; allPurpuseEntity.X = newX + i; allPurpuseEntity.Y = newY; break; } } if (foundedFreeSpot) { break; } for (int i = 0; i < levelElements; i++) { coord.X = newX + i; coord.Y = newY + levelElements - 1; if (!allCords.Any(t => t.X == coord.X && t.Y == coord.Y)) { allCords.Add(coord); foundedFreeSpot = true; allPurpuseEntity.X = newX + i; allPurpuseEntity.Y = newY + levelElements - 1; break; } } if (foundedFreeSpot) { break; } for (int i = 0; i < levelElements; i++) { coord.X = newX + levelElements - 1; coord.Y = newY + i; if (!allCords.Any(t => t.X == coord.X && t.Y == coord.Y)) { allCords.Add(coord); foundedFreeSpot = true; allPurpuseEntity.X = newX + levelElements - 1; allPurpuseEntity.Y = newY + i; break; } } if (foundedFreeSpot) { break; } levelElements += 2; level++; } } return(allPurpuseEntity); }