Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.10 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include<math.h>
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. #define NULL_VALUE -999999
  9. #define INFINITY 999999
  10. #define WHITE 1
  11. #define GREY 2
  12. #define BLACK 3
  13. #define MAX_HEAP_SIZE 100000
  14.  
  15.  
  16. #define MAXREAL 999999.0
  17.  
  18. class Queue
  19. {
  20. int queueInitSize ;
  21. int queueMaxSize;
  22. int * data;
  23. int length;
  24. int front;
  25. int rear;
  26. public:
  27. Queue();
  28. ~Queue();
  29. void enqueue(int item); //insert item in the queue
  30. int dequeue(); //returns the item according to FIFO
  31. bool empty(); //return true if Queue is empty
  32. };
  33.  
  34. Queue::Queue()
  35. {
  36. queueInitSize = 2 ;
  37. queueMaxSize = queueInitSize;
  38. data = new int[queueMaxSize] ; //allocate initial memory
  39. length = 0 ;
  40. front = 0;
  41. rear = 0;
  42. }
  43.  
  44.  
  45. void Queue::enqueue(int item)
  46. {
  47. if (length == queueMaxSize)
  48. {
  49. int * tempData ;
  50. //allocate new memory space for tempList
  51. queueMaxSize = 2 * queueMaxSize ;
  52. tempData = new int[queueMaxSize] ;
  53. int i, j;
  54. j = 0;
  55. for( i = rear; i < length ; i++ )
  56. {
  57. tempData[j++] = data[i] ; //copy items from rear
  58. }
  59. for( i = 0; i < rear ; i++ )
  60. {
  61. tempData[j++] = data[i] ; //copy items before rear
  62. }
  63. rear = 0 ;
  64. front = length ;
  65. delete[] data ; //free the memory allocated before
  66. data = tempData ; //make list to point to new memory
  67. }
  68.  
  69. data[front] = item ; //store new item
  70. front = (front + 1) % queueMaxSize ;
  71. length++ ;
  72. }
  73.  
  74.  
  75. bool Queue::empty()
  76. {
  77. if(length == 0) return true ;
  78. else return false ;
  79. }
  80.  
  81.  
  82. int Queue::dequeue()
  83. {
  84. if(length == 0) return NULL_VALUE ;
  85. int item = data[rear] ;
  86. rear = (rear + 1) % queueMaxSize ;
  87. length-- ;
  88. return item ;
  89. }
  90.  
  91.  
  92. Queue::~Queue()
  93. {
  94. if(data) delete[] data; //deallocate memory
  95. data = 0; //set to NULL
  96. }
  97.  
  98. //****************Queue class ends here************************
  99.  
  100. //****************Dynamic ArrayList class based************************
  101. class ArrayList
  102. {
  103. int * list;
  104. int length ;
  105. int listMaxSize ;
  106. int listInitSize ;
  107. public:
  108. ArrayList() ;
  109. ~ArrayList() ;
  110. int searchItem(int item) ;
  111. void insertItem(int item) ;
  112. void removeItem(int item) ;
  113. void removeItemAt(int item);
  114. int getItem(int position) ;
  115. int getLength();
  116. bool empty();
  117. void printList();
  118. } ;
  119.  
  120.  
  121. ArrayList::ArrayList()
  122. {
  123. listInitSize = 2 ;
  124. listMaxSize = listInitSize ;
  125. list = new int[listMaxSize] ;
  126. length = 0 ;
  127. }
  128.  
  129. void ArrayList::insertItem(int newitem)
  130. {
  131. int * tempList ;
  132. if (length == listMaxSize)
  133. {
  134. //allocate new memory space for tempList
  135. listMaxSize = 2 * listMaxSize ;
  136. tempList = new int[listMaxSize] ;
  137. int i;
  138. for( i = 0; i < length ; i++ )
  139. {
  140. tempList[i] = list[i] ; //copy all items from list to tempList
  141. }
  142. delete[] list ; //free the memory allocated before
  143. list = tempList ; //make list to point to new memory
  144. };
  145.  
  146. list[length] = newitem ; //store new item
  147. length++ ;
  148. }
  149.  
  150. int ArrayList::searchItem(int item)
  151. {
  152. int i = 0;
  153. for (i = 0; i < length; i++)
  154. {
  155. if( list[i] == item ) return i;
  156. }
  157. return NULL_VALUE;
  158. }
  159.  
  160. void ArrayList::removeItemAt(int position) //do not preserve order of items
  161. {
  162. if ( position < 0 || position >= length ) return ; //nothing to remove
  163. list[position] = list[length-1] ;
  164. length-- ;
  165. }
  166.  
  167.  
  168. void ArrayList::removeItem(int item)
  169. {
  170. int position;
  171. position = searchItem(item) ;
  172. if ( position == NULL_VALUE ) return ; //nothing to remove
  173. removeItemAt(position) ;
  174. }
  175.  
  176.  
  177. int ArrayList::getItem(int position)
  178. {
  179. if(position < 0 || position >= length) return NULL_VALUE ;
  180. return list[position] ;
  181. }
  182.  
  183. int ArrayList::getLength()
  184. {
  185. return length ;
  186. }
  187.  
  188. bool ArrayList::empty()
  189. {
  190. if(length==0)return true;
  191. else return false;
  192. }
  193.  
  194. void ArrayList::printList()
  195. {
  196. int i;
  197. for(i=0; i<length; i++)
  198. printf("%d ", list[i]);
  199. printf("Current size: %d, current length: %d\n", listMaxSize, length);
  200. }
  201.  
  202. ArrayList::~ArrayList()
  203. {
  204. if(list) delete [] list;
  205. list = 0 ;
  206. }
  207.  
  208. //******************ArrayList class ends here*************************
  209.  
  210. //******************Graph class starts here**************************
  211. class Graph
  212. {
  213. public:
  214. int nVertices, nEdges ;
  215. bool directed ;
  216. ArrayList * adjList ;
  217. //define other variables required for bfs such as color, parent, and dist
  218. //you must use pointers and dynamic allocation
  219. int *color;
  220. int *dist;
  221. int *parent;
  222. int time;
  223. int *discoveryTime;
  224. int *finishTime;
  225. int **weightArray;
  226. int **distance;
  227. int **path;
  228.  
  229. Graph(bool dir = false);
  230. ~Graph();
  231. void setnVertices(int n);
  232. void addEdge(int u, int v);
  233. void removeEdge(int u, int v);
  234. bool isEdge(int u, int v);
  235. int getDegree(int u);
  236. bool hasCommonAdjacent(int u, int v);
  237. int getDist(int u, int v);
  238. void printGraph();
  239. void bfs(int source); //will run bfs in the graph
  240. void dfs(); //will run dfs in the graph
  241. void dfs_visit(int u);
  242. void makeTranspose();
  243. void makeUndirected();
  244. void FloydWarshallAlgo();
  245. };
  246.  
  247.  
  248. Graph::Graph(bool dir)
  249. {
  250. nVertices = 0 ;
  251. nEdges = 0 ;
  252. adjList = 0 ;
  253. directed = dir ; //set direction of the graph
  254. color = 0;
  255. dist = 0;
  256. parent = 0;
  257. discoveryTime = 0;
  258. finishTime = 0;
  259. //define other variables to be initialized
  260. }
  261.  
  262. void Graph::setnVertices(int n)
  263. {
  264. this->nVertices = n ;
  265. if(adjList!=0) delete[] adjList ; //delete previous list
  266. adjList = new ArrayList[nVertices] ;
  267. weightArray = (int **) malloc(nVertices * sizeof(int *));
  268.  
  269. for (int i = 0; i < nVertices; i++)
  270. {
  271. weightArray[i] = (int *) malloc(nVertices * sizeof(int));
  272. }
  273.  
  274. for (int i = 0; i < nVertices; i++){
  275. for(int j=0;j<nVertices;j++)
  276. {
  277. if(i==j) weightArray[i][j] = 0;
  278. else weightArray[i][j] = 99999;
  279. }
  280. }
  281.  
  282. distance = (int **) malloc(nVertices * sizeof(int *));
  283.  
  284. for (int i = 0; i < nVertices; i++)
  285. {
  286. distance[i] = (int *) malloc(nVertices * sizeof(int));
  287. }
  288. for (int i = 0; i < nVertices; i++){
  289. for(int j=0;j<nVertices;j++)
  290. {
  291. if(i==j) distance[i][j] = 0;
  292.  
  293. else distance[i][j] = 99999;
  294. }
  295. }
  296. path = (int **) malloc(nVertices * sizeof(int *));
  297.  
  298. for (int i = 0; i < nVertices; i++)
  299. {
  300. path[i] = (int *) malloc(nVertices * sizeof(int));
  301. }
  302. for (int i = 0; i < nVertices; i++){
  303. for(int j=0;j<nVertices;j++)
  304. {
  305. path[i][j] = 99999;
  306. }
  307. }
  308. parent = new int[nVertices];
  309. }
  310.  
  311. void Graph::addEdge(int u, int v)
  312. {
  313. if(u<0 || v<0 || u>=nVertices || v>=nVertices) return; //vertex out of range
  314. this->nEdges++ ;
  315. adjList[u].insertItem(v) ;
  316. if(!directed) adjList[v].insertItem(u) ; //undirected holee
  317. }
  318.  
  319.  
  320.  
  321.  
  322. void Graph::FloydWarshallAlgo(){
  323.  
  324. for (int i = 0; i < nVertices ; i++){
  325. for (int j = 0; j < nVertices; j++){
  326. distance[i][j] = weightArray[i][j];
  327. if (distance[i][j] != 99999 && i != j) {
  328. path[i][j] = i;
  329. } else {
  330. path[i][j] = -1;
  331. }
  332. }
  333. }
  334.  
  335. for(int k=0; k < nVertices; k++){
  336. for(int i=0; i <nVertices; i++){
  337. for(int j=0; j < nVertices; j++){
  338. if(distance[i][k] == 99999 || distance[k][j] == 99999) {
  339. continue;
  340. }
  341. if(distance[i][j] > distance[i][k] + distance[k][j]){
  342. distance[i][j] = distance[i][k] + distance[k][j];
  343. path[i][j] = path[k][j];
  344.  
  345. }
  346. }
  347. }
  348. }
  349. for(int i = 0; i < nVertices; i++) {
  350. if(distance[i][i] < 0) {
  351. cout << "Negative cycle present" ;
  352. }
  353. }
  354. for (int i = 0; i < nVertices ; i++){
  355. for (int j = 0; j < nVertices; j++)
  356. {
  357. if(weightArray[i][j]==99999) cout<< "INF" <<" ";
  358. else
  359. cout<< weightArray[i][j]<<" ";
  360. }
  361. cout <<endl;
  362.  
  363. }
  364. for (int i = 0; i < nVertices ; i++){
  365. for (int j = 0; j < nVertices; j++)
  366. {
  367. if(distance[i][j]==99999) cout<< "INF" <<" ";
  368. else cout << distance[i][j]<<" ";
  369. }
  370. cout << endl;
  371.  
  372. }
  373. for (int i = 0; i < nVertices ; i++){
  374. for (int j = 0; j < nVertices; j++)
  375. {
  376. if(path[i][j]==-1) cout<< "INF" <<" ";
  377. else
  378. cout<< path[i][j]+1<<" ";
  379. }
  380. cout <<endl;
  381.  
  382. }
  383.  
  384. int i=0;
  385. int j=1;
  386. cout << "path between" << i+1 << " " << j+1<<endl;
  387. stack <int> Stack;
  388. Stack.push(j);
  389. cout << j+1<<" ";
  390. int a = i;
  391. int b = j;
  392. while (true) {
  393.  
  394. b = path[a][b];
  395. if(b == -1) {
  396. return;
  397. }
  398. Stack.push(b);
  399. cout << b+1<<" ";
  400. if(b == a) {
  401. break;
  402. }
  403.  
  404. }
  405. /// (u,v) edge thaktei hobe
  406. /// (p,q) er majhe notun path ber korte hobe jetate (u,v) thakbe
  407. int u=0;
  408. int v=2;
  409. int p=0;
  410. int q=3;
  411. // for (int i = 0; i < nVertices ; i++){
  412. // for (int j = 0; j < nVertices; j++){
  413. // distance[i][j] = weightArray[i][j];
  414. //
  415. // }
  416. //}
  417.  
  418. for(int k=0; k < nVertices; k++){
  419.  
  420. // if(distance[i][j] > distance[i][p] + distance[p][q]+distance[q][j]){
  421. distance[p][q] = distance[p][u] + weightArray[u][v]+distance[v][q];
  422. // path[i][j] = path[k][j];
  423.  
  424.  
  425. }
  426. cout << "dis: "<<distance[p][q];
  427. // for(int k=0; k < nVertices; k++){
  428. // if(distance[p][k] == 99999 || distance[k][q] == 99999) {
  429. // continue;
  430. // }
  431. // if(distance[p][q] > distance[p][u] +distance[u][v]+distance[v][q]){
  432. // distance[p][q] =distance[p][u] +distance[u][v]+distance[v][q];
  433. //
  434. // }
  435. // }
  436. // cout << "new dist: "<<distance[i][j];
  437.  
  438.  
  439. // while(!Stack.empty()){
  440. // int a = Stack.pop();
  441. // printf("%d ",a);
  442. // }
  443.  
  444.  
  445.  
  446.  
  447. }
  448. void Graph::printGraph()
  449. {
  450. printf("\nNumber of vertices: %d, Number of edges: %d\n", nVertices, nEdges);
  451. for(int i=0; i<nVertices; i++)
  452. {
  453. printf("%d:", i+1);
  454. for(int j=0; j<adjList[i].getLength(); j++)
  455. {
  456. printf(" %d", adjList[i].getItem(j)+1);
  457. }
  458. printf("\n");
  459. }
  460. }
  461.  
  462. Graph::~Graph()
  463. {
  464. //write your destructor here
  465. delete [] color;
  466. delete [] dist;
  467. delete [] parent;
  468. delete [] finishTime;
  469. delete [] discoveryTime;
  470. delete [] adjList;
  471. }
  472.  
  473.  
  474. //**********************Graph class ends here******************************
  475.  
  476.  
  477. //******main function to test your code*************************
  478. int main(void)
  479. {
  480. int n,m;
  481. ifstream in ("testcasefloyd.txt");
  482. Graph g(true);
  483. int u,v,w;
  484. // scanf("%d", &n);
  485. in >> n;
  486. in >> m;
  487. g.setnVertices(n);
  488. // g.setnEdges(m);
  489. for(int i=0; i<m; i++)
  490. {
  491. in >> u;
  492. in >> v;
  493. in >> w;
  494. if ( g.weightArray[u-1][v-1]==-1)
  495. {
  496. g.weightArray[u-1][v-1] = w;
  497. if(g.directed==false) g.weightArray[v-1][u-1] = w;
  498. g.addEdge(u-1,v-1);
  499. }
  500. else if(g.weightArray[u-1][v-1]>w)
  501. {
  502. g.weightArray[u-1][v-1] = w;
  503. if(g.directed==false) g.weightArray[v-1][u-1] = w;
  504. g.addEdge(u-1,v-1);
  505. }
  506. }
  507. g.FloydWarshallAlgo();
  508.  
  509. //g.printGraph();
  510.  
  511. // Graph MST =
  512.  
  513. return 0;
  514.  
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement