public static Graph<int> DijkstrasAlg(Graph<int> graph) { List<Node<int>> nodeset = graph.nodeset.ToList<Node<int>>(); Graph<int> sptGraph = new Graph<int>(); while (nodeset.Count > 0) { Minheapify(nodeset); Node<int> root = nodeset[0]; nodeset.RemoveAt(0); Node<int> sptNode = new Node<int>(root.data); if (root.parent != null) { sptGraph.AddEdge(sptNode, root.parent, root.key); root.parent = null; } for (int i = 0; i < root.neighbors.Count; i++) { if (root.neighbors[i].key > (root.cost[i] + root.key)) { root.neighbors[i].key = root.cost[i] + root.key; root.neighbors[i].parent = sptNode; } } } sptGraph.printGraph(); return sptGraph; }
static void Main(string[] args) { var n01 = new Node("01"); var n02 = new Node("02"); var n03 = new Node("03"); var n04 = new Node("04"); var n05 = new Node("05"); var n06 = new Node("06"); var n07 = new Node("07"); var n08 = new Node("08"); var n09 = new Node("09"); var n10 = new Node("10"); var n11 = new Node("11"); var n12 = new Node("12"); var n13 = new Node("13"); var n14 = new Node("14"); var n15 = new Node("15"); n01.AddChildren(n02).AddChildren(n03); n02.AddChildren(n05); n03.AddChildren(n04); n04.AddChildren(n05, false).AddChildren(n10, false).AddChildren(n11, false); n06.AddChildren(n01, false); n07.AddChildren(n03, false).AddChildren(n08); n09.AddChildren(n08).AddChildren(n10); n11.AddChildren(n12).AddChildren(n13); n12.AddChildren(n13); n14.AddChildren(n15); var search = new DepthFirstSearch(); var path = search.DLS(n06, n13,6); PrintPath(path); }
//Time: O(E logV) using adjacency list rep (using BFS on keys) //Find minimum spanning tree (connected nodes with min cost edges, E = V-1) //1. Place the graph in min heap (using keys) //2. Extract the min key node and add to MST graph //3. Update the keys of adjacent nodes (if key > cost then update) //4. Minheapify the remaining nodes in the graph and repeat from step 2 until the entire list is empty private static Graph<int> PrimsAlg(Graph<int> graph) { Graph<int> mstGraph = new Graph<int>(); //Generate a new MST graph List<Node<int>> nodeset = graph.nodeset.ToList<Node<int>>(); //Create a new list of nodes from existing graph, so that we don't modify the given graph while (nodeset.Count > 0) { Minheapify(nodeset); //Place the list in MinHeap Node<int> root = nodeset[0]; //Extract Min node Node<int> mstNode = new Node<int>(root.data); //Create a new node for MST graph if (root.parent != null) //Add nodes and edges between them { mstGraph.AddEdge(mstNode, root.parent, root.key); root.parent = null; //reset the parent node } nodeset.RemoveAt(0); //Extract root node (remove from list) for(int i = 0; i< root.neighbors.Count; i++) { if (root.neighbors[i].key > root.cost[i]) //Update keys of neighboring nodes if the min node { root.neighbors[i].key = root.cost[i]; root.neighbors[i].parent = mstNode; } } } mstGraph.printGraph(); return mstGraph; }
public Node AddChildren(Node node, bool bidirect = true) { Children.Add(node); if (bidirect) { node.Children.Add(this); } return this; }
public LinkedList<Node> IDDFS(Node start, Node goal) { for (int limit = 1; ; limit++) { var result = DLS(start, goal, limit); if (result.Count > 0 || limitWasReached) { return result; } } }
public LinkedList<Node> DFS(Node start, Node goal) { visited = new HashSet<Node>(); path = new LinkedList<Node>(); this.goal = goal; DFS(start); if (path.Count > 0) { path.AddFirst(start); } return path; }
public LinkedList<Node> DLS(Node start, Node goal, int limit) { visited = new HashSet<Node>(); path = new LinkedList<Node>(); limitWasReached = true; this.goal = goal; DLS(start, limit); if (path.Count > 0) { path.AddFirst(start); } return path; }
public void ConnectNode(Node start, Node end) { if (_adjMatrix == null) { _size = _nodes.Count; _adjMatrix = new int[_size, _size]; } int startIndex = _nodes.IndexOf(start); int endIndex = _nodes.IndexOf(end); _adjMatrix[startIndex, endIndex] = 1; _adjMatrix[endIndex, startIndex] = 1; }
private bool DFS(Node node) { node.Handler(); if (node == goal) { return true; } visited.Add(node); foreach (var child in node.Children.Where(x => !visited.Contains(x))) { if (DFS(child)) { path.AddFirst(child); return true; } } return false; }
private bool DLS(Node node, int limit) { node.Handler(); if (node == goal) { return true; } if (limit == 0) { limitWasReached = false; return false; } visited.Add(node); foreach (var child in node.Children.Where(x => !visited.Contains(x))) { if (DLS(child, limit - 1)) { path.AddFirst(child); return true; } } return false; }
public Graph(Edge[] edges, Node[] nodes) { this.edges = edges; this.nodes = nodes; }
public override void ForEach(Action <StackSourceSample> callback) { // Initialize the priority if (m_typePriorities == null) { PriorityRegExs = DefaultPriorities; } Debug.Assert(m_typePriorities != null); // Initialize the breadth-first work queue. var nodesToVisit = new PriorityQueue(1024); nodesToVisit.Enqueue(m_graph.RootIndex, 0.0F); // reset the visited information. for (int i = 0; i < m_parent.Length; i++) { m_parent[i] = NodeIndex.Invalid; } // We keep track of node depth so that we can limit it. int[] nodeDepth = new int[m_parent.Length]; float[] nodePriorities = new float[m_parent.Length]; MemoryGraph asMemoryGraph = m_graph as MemoryGraph; bool scanedForOrphans = false; var epsilon = 1E-7F; // Something that is big enough not to bet lost in roundoff error. float order = 0; for (int i = 0; ; i++) { if ((i & 0x1FFF) == 0) // Every 8K { System.Threading.Thread.Sleep(0); // Allow interruption. } NodeIndex nodeIndex; float nodePriority; if (nodesToVisit.Count == 0) { nodePriority = 0; if (!scanedForOrphans) { scanedForOrphans = true; AddOrphansToQueue(nodesToVisit); } if (nodesToVisit.Count == 0) { return; } } nodeIndex = nodesToVisit.Dequeue(out nodePriority); // Insert any children that have not already been visited (had a parent assigned) into the work queue). Node node = m_graph.GetNode(nodeIndex, m_nodeStorage); var parentPriority = nodePriorities[(int)node.Index]; for (var childIndex = node.GetFirstChildIndex(); childIndex != NodeIndex.Invalid; childIndex = node.GetNextChildIndex()) { if (m_parent[(int)childIndex] == NodeIndex.Invalid && childIndex != m_graph.RootIndex) { m_parent[(int)childIndex] = nodeIndex; int parentDepth = nodeDepth[(int)nodeIndex]; nodeDepth[(int)childIndex] = checked (parentDepth + 1); // the priority of the child is determined by its type and 1/10 by its parent. var child = m_graph.GetNode(childIndex, m_childStorage); var childPriority = m_typePriorities[(int)child.TypeIndex] + parentPriority / 10; nodePriorities[(int)childIndex] = childPriority; // Subtract a small increasing value to keep the queue in order if the priorities are the same. // This is a bit of a hack since it can get big and perturb the user-defined order. order += epsilon; nodesToVisit.Enqueue(childIndex, childPriority - order); } } // Return the node. m_sampleStorage.Metric = node.Size; // We use the address as the timestamp. This allows you to pick particular instances // and see where particular instances are in memory by looking at the 'time'. if (asMemoryGraph != null) { m_sampleStorage.TimeRelativeMSec = asMemoryGraph.GetAddress(node.Index); } m_sampleStorage.SampleIndex = (StackSourceSampleIndex)node.Index; m_sampleStorage.StackIndex = (StackSourceCallStackIndex)node.Index; if (m_countMultipliers != null) { m_sampleStorage.Count = m_countMultipliers[(int)node.TypeIndex]; m_sampleStorage.Metric = node.Size * m_sampleStorage.Count; } Debug.Assert(m_sampleStorage.Metric >= 0); Debug.Assert(m_sampleStorage.Count >= 0); Debug.Assert(0 < m_sampleStorage.Count && m_sampleStorage.Count <= float.MaxValue); Debug.Assert(0 <= m_sampleStorage.Metric && m_sampleStorage.Metric <= float.MaxValue); callback(m_sampleStorage); } }
/// <summary> /// Gives back nodes that are no longer in use. This is a memory optimization. /// </summary> private void FreeNodeStorage(Node node) { m_cachedNodeStorage = node; }
public Edge(int w, Node u, Node v) { this.weight = w; this.u = u; this.v = v; }
public void SetRootNode(Node n) { this._rootNode = n; }
public void AddNode(Node n) { _nodes.Add(n); }
private Node GetUnvisitedChildNode(Node n) { int index = _nodes.IndexOf(n); int j = 0; while (j < _size) { if (_adjMatrix[index, j] == 1 && _nodes[j].visited == false) { return _nodes[j]; } j++; } return null; }