Advertisement
UmbertoKing

Untitled

Jun 15th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.27 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <assert.h>
  5. #include <time.h>
  6. #include "Alberi BST/Tree.h"
  7. #include "Code/queue.h"
  8. #include "Grafi/List.h"
  9. #include "Grafi/Graph.h"
  10. #define HEAP_MAX 128
  11.  
  12. struct THeap{
  13.     int A[HEAP_MAX];
  14.     int heapsize;
  15. };
  16.  
  17. typedef struct THeap* Heap;
  18.  
  19. Heap nuovoHeap(){
  20.     Heap ret = (Heap)malloc(sizeof(struct THeap));
  21.     ret->heapsize = 0;
  22.     return ret;
  23. }
  24.  
  25. void swapHeap(Heap coda, int x, int y){
  26.     assert(coda != NULL);
  27.     int aux = coda->A[x];
  28.     coda->A[x] = coda->A[y];
  29.     coda->A[y] = aux;
  30. }
  31.  
  32. void upHeap(Heap coda, int i, int* p){
  33.     assert(coda != NULL && p != NULL);
  34.     if(i <= 0) return;
  35.     int dad = (i-1)/2;
  36.     if (p[coda->A[dad]] > p[coda->A[i]]){
  37.         swapHeap(coda, dad, i);
  38.         upHeap(coda, dad, p);
  39.     }
  40. }
  41.  
  42. void insertHeap(Heap coda, int i, int* p){
  43.     assert(coda != NULL && p != NULL);
  44.     if (coda->heapsize >= HEAP_MAX) return;
  45.     coda->A[coda->heapsize] = i;
  46.     upHeap(coda, coda->heapsize, p);
  47.     coda->heapsize += 1;
  48. }
  49.  
  50. void Heapify(Heap coda, int i, int* p){
  51.     assert(coda != NULL && p != NULL);
  52.     int lesser = i;
  53.     int left = (2*i)+1;
  54.     int right = (2*i)+2;
  55.     if (left < coda->heapsize && p[coda->A[left]] < p[coda->A[lesser]])
  56.         lesser = left;
  57.     if (right < coda->heapsize &&  p[coda->A[right]] < p[coda->A[lesser]])
  58.         lesser = right;
  59.     if(i != lesser){
  60.         swapHeap(coda, lesser, i);
  61.         Heapify(coda, lesser, p);
  62.     }
  63. }
  64.  
  65. int popHeap(Heap coda, int* p){
  66.     assert(coda != NULL && p != NULL);
  67.     if(!coda->heapsize) return 0;
  68.     int ret = coda->A[0];
  69.     coda->heapsize -= 1;
  70.     coda->A[0] = coda->A[coda->heapsize];
  71.     Heapify(coda, 0, p);
  72.     return ret;
  73. }
  74.  
  75.  
  76. int err;
  77.  
  78. int rimuovi(Queue Q);
  79. int* Dijkstra(Graph G, int s);
  80. void  enhead(Queue Q, int value, int *err);
  81. void togli_numero(List lista1, List lista2);
  82.  
  83. int main(){
  84.     srand(time(NULL
  85.     ));
  86.     Graph G = randomGraph(11, 5, 11);
  87.     printGraph(G);
  88.     printf("\n\n");
  89.     int* pred = Dijkstra(G, 0);
  90.     int cur = 10;
  91.     while(cur != 0 && cur != -1){
  92.         printf("%d << ", cur);
  93.         cur = pred[cur];
  94.     }
  95.     if (cur == 0) printf("%d", cur);
  96.     else printf("irraginugibile");
  97.     return 0;
  98. }
  99.  
  100. int main1(){
  101.     Queue Q = initQueue();
  102.     enqueue(Q, 7, &err);
  103.     enqueue(Q, 1, &err);
  104.     enqueue(Q, 4, &err);
  105.     enqueue(Q, 3, &err);
  106.     enqueue(Q, 2, &err);
  107.     enqueue(Q, 5, &err);
  108.     printQueue(Q, &err);
  109.     printf("\n\n");
  110.     rimuovi(Q);
  111.     printQueue(Q, &err);
  112.     return 0;
  113. }
  114.  
  115. int main2(){
  116.     srand(time(NULL));
  117.     List lista1 = randomList(20, 10);
  118.     List lista2 = randomList(20, 10);
  119.     printList(lista1);
  120.     printf("\n\n");
  121.     printList(lista2);
  122.     printf("\n\n");
  123.     togli_numero(lista1, lista2);
  124.     printList(lista1);
  125.     printf("\n\n");
  126.     printList(lista2);
  127.     printf("\n\n");
  128.     return 0;
  129. }
  130.  
  131. int* Dijkstra(Graph G, int s){
  132.     if(!G) return NULL;
  133.     int dim = G->nodes_count;
  134.     int* pred = (int*)malloc(sizeof(int)*dim);
  135.     int dist[dim];
  136.     Heap coda = nuovoHeap();
  137.     int i;
  138.     for(i=0;i<dim; i++){
  139.         pred[i] = -1;
  140.         if (i == s) dist[i] = 0;
  141.         else dist[i] = INT_MAX;
  142.         insertHeap(coda, i, dist);
  143.     }
  144.     int u, v, alt;
  145.     List curr;
  146.     while(coda->heapsize){
  147.         u = popHeap(coda, dist);
  148.         curr = G->adj[u];
  149.         while(curr){
  150.             v = curr->target;
  151.             alt = dist[u] + curr->peso;
  152.             if( alt < dist[v]){
  153.                 dist[v] = alt;
  154.                 pred[v] = u;
  155.                 upHeap(coda, v, dist);
  156.             }
  157.             curr = curr->next;
  158.         }
  159.     }
  160.     return pred;
  161. }
  162.  
  163. List removeTutt(List L, int target, int* nop) {
  164.     if (L != NULL) {
  165.         if (L->target == target) {
  166.             List tmp = L->next;
  167.             free(L);
  168.             (*nop) = 1;
  169.             tmp->next = removeTutt(tmp->next, target, nop);
  170.             return tmp;
  171.         }
  172.         L->next = removeTutt(L->next, target, nop);
  173.     }
  174.     return L;
  175. }
  176.  
  177. void togli_numero(List lista1, List lista2){
  178.     int m, nop = 1;
  179.     List app, l1 = lista1, l2 = lista2;
  180.     while(l1 && l2 && nop){
  181.         nop = 0;
  182.         m = sizeList(l1);
  183.         l2 = removeTutt(l2, m, &nop);
  184.         app = l1;
  185.         l1 = l2;
  186.         l2 = app;
  187.     }
  188. }
  189.  
  190. int rimuovi(Queue Q){
  191.     if(!Q || emptyQueue(Q)) return INT_MAX;
  192.     int pos = sizeQueue(Q);
  193.     int el;
  194.     el = dequeue(Q, &err);
  195.     int val = rimuovi(Q);
  196.  
  197.     if(el%2 == 0 && pos%2 == 0 && el > val){}
  198.     else if(el%2 != 0 && pos%2 != 0){}
  199.     else{
  200.         enhead(Q, el, &err);
  201.         //enqueue(Q, el, &err);
  202.     }
  203.     return el;
  204. }
  205.  
  206. void  enhead(Queue Q, int value, int *err){
  207.     if(!Q || fullQueue(Q)) return;
  208.     int i, el;
  209.     int siz =  sizeQueue(Q);
  210.     enqueue(Q, value, err);
  211.     for(i=0; i< siz; i++){
  212.         el = dequeue(Q, err);
  213.         enqueue(Q, el, err);
  214.     }
  215. //    if (!fullQueue(Q)) {
  216. //        if (Q->A[0] > 1) Q->A[0] -= 1;
  217. //        else Q->A[0] = QUEUE_MAX-1;
  218. //        Q->A[Q->A[0]] = value; // Inserisci valore in coda
  219. //        if (emptyQueue(Q)) {
  220. //            Q->A[0] = 1; // Se e' vuota, sposto la testa in prima posizione
  221. //        }
  222. //        *err = 0;
  223. //    } else {
  224. //        *err = 5;
  225. //    }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement