public static void calculate(Graph g, int r) { /* init vertices list. */ for (int i = 0; i < g.n_v; i++) { g.v[i].key = float.MaxValue; // g.v[i].distance2D = float.MaxValue; g.v[i].parent = -1; } /* Set root. */ g.v[r].key = 0; /* Prepare queue of type min. */ //FibonacciHeap q = new FibonacciHeap(g); NaiveProrityQueue q = new NaiveProrityQueue(g); /* Until queue is empty. */ while (!q.isQueueEmpty()) { FibonacciNode u = q.extractMin(); Console.Write("" + u.key); g.v[u.index].inQ = false; LinkedListNode tmp = g.adj.getVertex(u.index).root; while (tmp != null) { if (g.v[tmp.index].inQ && g.wageFunction(u.index, tmp.index) < g.v[tmp.index].key) { g.v[tmp.index].parent = u.index; q.decreaseKey(g.v[tmp.index], g.wageFunction(u.index, tmp.index), g.wage2D(u.index, tmp.index)); } tmp = tmp.next; } } }
/** * During returning minimum element, heap's roots list is shrunk * using {@link #consolidate()} method. * * @return Minimal element of the heap. */ public FibonacciNode extractMin() { FibonacciNode z = min; if (z != null) { /* To avoid NullPointerException. */ //if (z.children == null) z.children = new DoublyLinkedList(); for (int i = 0; i < z.children.Count; i++) { FibonacciNode x = z.children.First(); x.p = null; z.children.RemoveAt(0); roots.Add(x); } roots.Remove(z); /* Heap without z is empty. */ if (roots.Count == 0) this.min = null; /* Or... */ else { this.min = roots.First(); consolidate(); } this.n--; } return z; }
/** * During returning minimum element, heap's roots list is shrunk * using {@link #consolidate()} method. * * @return Minimal element of the heap. */ public FibonacciNode extractMin() { FibonacciNode z = min; if (z != null) { /* To avoid NullPointerException. */ //if (z.children == null) z.children = new DoublyLinkedList(); for (int i = 0; i < z.children.Count; i++) { FibonacciNode x = z.children.First(); x.p = null; z.children.RemoveAt(0); roots.Add(x); } roots.Remove(z); /* Heap without z is empty. */ if (roots.Count == 0) { this.min = null; } /* Or... */ else { this.min = roots.First(); consolidate(); } this.n--; } return(z); }
public FibonacciNode extractFirst() { FibonacciNode tmp = guard.next; guard.next = guard.next.next; guard.next.prev = guard; return(tmp); }
public void insert(FibonacciNode x) { x.next = guard.next; guard.next.prev = x; guard.next = x; x.prev = guard; elements++; }
private void cut(FibonacciNode x, FibonacciNode y) { y.children.Remove(x); y.degree--; roots.Add(x); x.p = null; x.mark = false; }
/** * Node y becomes x's child. * * @param y * @param x */ private void link(FibonacciNode y, FibonacciNode x) { roots.Remove(y); //if (x.children == null) x.children = new DoublyLinkedList(); x.children.Add(y); x.degree++; y.p = x; y.mark = false; }
public void decreaseKey(FibonacciNode node, float key, float distance2D) { if (node.key < key || key < 0) { return; } node.key = key; node.distance2D = distance2D; }
public Graph(int n_v) { this.n_v = n_v; /* New adjacency list. */ this.adj = new AdjacencyList(n_v); /* Init vertices list. */ this.v = new FibonacciNode[n_v]; for (int i = 0; i < n_v; i++) v[i] = new FibonacciNode(i); }
public void print() { FibonacciNode x = guard.next; while (x != guard) { x = x.next; } Console.WriteLine(); }
public FibonacciNode search(FibonacciNode k) { FibonacciNode x = guard.next; while (x != guard && x.key != k.key) { x = x.next; } return(x); }
public FibonacciNode extractMin() { FibonacciNode min = new FibonacciNode(); min.key = float.MaxValue; foreach (FibonacciNode node in list) { if (node.key < min.key) min = node; } list.Remove(min); return min; }
private static void traverse(Graph graph, FibonacciNode node, List <int> route, int index) { route.Add(node.index); nieUmiemCSharpaWiecJestTaZmienna += node.distance2D; for (int i = 0; i < graph.v.Length; i++) { if (graph.v[i].parent == node.index) { traverse(graph, graph.v[i], route, ++index); } } }
public Graph(int n_v) { this.n_v = n_v; /* New adjacency list. */ this.adj = new AdjacencyList(n_v); /* Init vertices list. */ this.v = new FibonacciNode[n_v]; for (int i = 0; i < n_v; i++) { v[i] = new FibonacciNode(i); } }
/** * Tu się dopiero papieże wyprawiają... */ private void consolidate() { //int upperBound = upperBoundForDegree(this.n + 1); FibonacciNode[] A = new FibonacciNode[500]; for (int i = 0; i < A.Length; i++) { A[i] = null; } for (int i = 0; i < roots.Count; i++) { FibonacciNode node = roots.ElementAt(i); int d = node.degree; while (A[d] != null) { /* Another node with the same degree as x. */ FibonacciNode y = A[d]; /* Swap x with y. */ if (node.key > y.key) { swap(node, y); } link(y, node); A[d] = null; d++; } A[d] = node; } this.min = null; for (int i = 0; i < 500; i++) { if (A[i] != null) { if (this.min == null) { this.min = A[i]; } else { roots.Add(A[i]); if (A[i].key < this.min.key) { this.min = A[i]; } } } } }
public void insert(FibonacciNode x) { if (this.min == null) { roots.Add(x); this.min = x; } else { roots.Add(x); if (x.key < this.min.key) this.min = x; } this.n++; }
public FibonacciNode extractMin() { FibonacciNode min = new FibonacciNode(); min.key = float.MaxValue; foreach (FibonacciNode node in list) { if (node.key < min.key) { min = node; } } list.Remove(min); return(min); }
public void insert(FibonacciNode x) { if (this.min == null) { roots.Add(x); this.min = x; } else { roots.Add(x); if (x.key < this.min.key) { this.min = x; } } this.n++; }
private void cascading(FibonacciNode y) { FibonacciNode z = y.p; if (z != null) { if (y.mark == false) { y.mark = true; } else { cut(y, z); cascading(z); } } }
/** * Decreases key attribute of a given node. * * @param x * @param k */ public void decreaseKey(FibonacciNode x, float k) { if (k > x.key) { return; } x.key = k; FibonacciNode y = x.p; if (y != null && x.key < y.key) { cut(x, y); cascading(y); } if (x.key < this.min.key) { this.min = x; } }
void swap(FibonacciNode f1, FibonacciNode f2) { FibonacciNode f3 = f2; f1.index = f2.index; f1.key = f2.key; f1.next = f2.next; f1.prev = f2.prev; f1.p = f2.p; f1.mark = f2.mark; f1.inQ = f2.inQ; f1.children = f2.children; // f1.degree = f2.degree; f2.index = f3.index; f2.key = f3.key; f2.next = f3.next; f2.prev = f3.prev; f2.p = f3.p; f2.mark = f3.mark; f2.inQ = f3.inQ; f2.children = f3.children; // f2.degree = f3.degree; }
/** * Decreases key attribute of a given node. * * @param x * @param k */ public void decreaseKey(FibonacciNode x, float k) { if (k > x.key) return; x.key = k; FibonacciNode y = x.p; if (y != null && x.key < y.key) { cut(x, y); cascading(y); } if (x.key < this.min.key) this.min = x; }
private static void traverse(Graph graph, FibonacciNode node, List<int> route, int index) { route.Add(node.index); nieUmiemCSharpaWiecJestTaZmienna += node.distance2D; for (int i = 0; i < graph.v.Length; i++) { if (graph.v[i].parent == node.index) traverse(graph, graph.v[i], route, ++index); } }
public DoublyLinkedList() { guard = new FibonacciNode(); guard.next = guard; guard.prev = guard; }
/** * Tu się dopiero papieże wyprawiają... */ private void consolidate() { //int upperBound = upperBoundForDegree(this.n + 1); FibonacciNode[] A = new FibonacciNode[500]; for (int i = 0; i < A.Length; i++) A[i] = null; for(int i = 0; i < roots.Count; i++) { FibonacciNode node = roots.ElementAt(i); int d = node.degree; while (A[d] != null) { /* Another node with the same degree as x. */ FibonacciNode y = A[d]; /* Swap x with y. */ if (node.key > y.key) { swap(node, y); } link(y, node); A[d] = null; d++; } A[d] = node; } this.min = null; for (int i = 0; i < 500; i++) { if (A[i] != null) { if (this.min == null) { this.min = A[i]; } else { roots.Add(A[i]); if (A[i].key < this.min.key) this.min = A[i]; } } } }
public void delete(FibonacciNode x) { x.prev.next = x.next; x.next.prev = x.prev; elements--; }
public void decreaseKey(FibonacciNode node, float key, float distance2D) { if (node.key < key || key < 0) return; node.key = key; node.distance2D = distance2D; }
private void cascading(FibonacciNode y) { FibonacciNode z = y.p; if (z != null) { if (y.mark == false) y.mark = true; else { cut(y, z); cascading(z); } } }
public FibonacciNode search(FibonacciNode k) { FibonacciNode x = guard.next; while (x != guard && x.key != k.key) x = x.next; return x; }