Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.74 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. void Graph::removeEdge(int u, int v)
  320. {
  321. if(u<0 || v<0 || u>=nVertices || v>=nVertices) return; //vertex out of range
  322.  
  323. adjList[u].removeItem(v) ;
  324. this->nEdges-- ;
  325. if(!directed) adjList[v].removeItem(u) ;
  326. }
  327.  
  328. bool Graph::isEdge(int u, int v)
  329. {
  330. //returns true if (u,v) is an edge, otherwise should return false
  331. if(u<0 || v<0 || u>=nVertices || v>=nVertices) return false; //vertex out of range
  332.  
  333. int p = adjList[u].searchItem(v) ;
  334. int q = adjList[v].searchItem(u) ;
  335. printf("p= %d and q = %d",p,q);
  336. if (p==NULL_VALUE && q==NULL_VALUE)
  337. {
  338. printf("false\n");
  339. return false;
  340. }
  341. else
  342. {
  343. printf("true\n");
  344. return true;
  345. }
  346. }
  347.  
  348. int Graph::getDegree(int u)
  349. {
  350. if(u<0 || u>=nVertices ) return NULL_VALUE;
  351.  
  352. return adjList[u].getLength();
  353. //returns the degree of vertex u
  354. }
  355.  
  356. bool Graph::hasCommonAdjacent(int u, int v)
  357. {
  358. //returns true if vertices u and v have common adjacent vertices
  359. int N = adjList[u].getLength();
  360. int a[N];
  361. for(int j=0; j<adjList[u].getLength(); j++)
  362. {
  363. a[j] = adjList[u].getItem(j);
  364. }
  365. int c = 0;
  366. for(int k=0; k<adjList[u].getLength(); k++)
  367. {
  368. for(int j=0; j<adjList[v].getLength(); j++)
  369. {
  370. if( adjList[u].getItem(k) == adjList[v].getItem(j))
  371. {
  372. c++;
  373. }
  374. }
  375. }
  376. if(c>0) return true;
  377. else return false;
  378.  
  379. }
  380.  
  381. void Graph::bfs(int source)
  382. {
  383. //complete this function
  384. //initialize BFS variables
  385. color = new int[nVertices];
  386. dist = new int[nVertices];
  387. parent = new int[nVertices];
  388. int p;
  389. for(int i=0; i<nVertices; i++)
  390. {
  391. color[i] = WHITE ;
  392. parent[i] = -1 ;
  393. dist[i] = INFINITY ;
  394. }
  395. Queue q ;
  396. color[source] = GREY;
  397. dist[source] = 0 ;
  398. q.enqueue(source) ;
  399. while( !q.empty() )
  400. {
  401. p = q.dequeue();
  402. for(int j = 0; j< adjList[p].getLength(); j++)
  403. {
  404. if(color[adjList[p].getItem(j)]==WHITE)
  405. {
  406. color[adjList[p].getItem(j)] = GREY;
  407. dist[adjList[p].getItem(j)] = dist[p] + 1;
  408. parent[adjList[p].getItem(j)] = p;
  409. q.enqueue(adjList[p].getItem(j)) ;
  410. }
  411. color[p] = BLACK;
  412.  
  413.  
  414. }
  415.  
  416. }
  417. for(int i=0; i<nVertices; i++) printf("%d",color[i]);
  418. }
  419.  
  420. void Graph::dfs()
  421. {
  422. color = new int[nVertices];
  423. parent = new int[nVertices];
  424. discoveryTime = new int[nVertices];
  425. finishTime = new int[nVertices];
  426. for(int i=0; i<nVertices; i++)
  427. {
  428. color[i] = WHITE ;
  429. parent[i] = -1 ;
  430. }
  431. time = 0;
  432.  
  433. for(int u = 0; u< nVertices; u++)
  434. {
  435. if(color[u]==WHITE)
  436. {
  437. dfs_visit(u);
  438. }
  439. }
  440. for(int i = 0; i< nVertices; i++)
  441. {
  442.  
  443. printf("Vertex: %d ->dist: %d ->finishing time: %d\n",i,discoveryTime[i],finishTime[i]);
  444.  
  445. }
  446.  
  447.  
  448. }
  449. void Graph::dfs_visit(int u)
  450. {
  451. //write this function
  452.  
  453. time = time + 1 ;
  454. discoveryTime[u] = time;
  455. color[u] = GREY;
  456. for(int j = 0; j< adjList[u].getLength(); j++)
  457. {
  458. if(color[adjList[u].getItem(j)]==WHITE)
  459. {
  460. parent[adjList[u].getItem(j)] = u;
  461. dfs_visit(adjList[u].getItem(j));
  462. }
  463.  
  464.  
  465. }
  466.  
  467. color[u] = BLACK;
  468. time = time +1;
  469. finishTime[u] = time;
  470.  
  471.  
  472. }
  473. int Graph::getDist(int u, int v)
  474. {
  475. //returns the shortest path distance from u to v
  476. //must call bfs using u as the source vertex, then use distance array to find the distance
  477. bfs(u) ;
  478. return dist[v];
  479. }
  480.  
  481. void Graph::FloydWarshallAlgo(){
  482.  
  483. for (int i = 0; i < nVertices ; i++){
  484. for (int j = 0; j < nVertices; j++){
  485. distance[i][j] = weightArray[i][j];
  486. if (distance[i][j] != 99999 && i != j) {
  487. path[i][j] = i;
  488. } else {
  489. path[i][j] = -1;
  490. }
  491. }
  492. }
  493.  
  494. for(int k=0; k < nVertices; k++){
  495. for(int i=0; i <nVertices; i++){
  496. for(int j=0; j < nVertices; j++){
  497. if(distance[i][k] == 99999 || distance[k][j] == 99999) {
  498. continue;
  499. }
  500. if(distance[i][j] > distance[i][k] + distance[k][j]){
  501. distance[i][j] = distance[i][k] + distance[k][j];
  502. path[i][j] = path[k][j];
  503.  
  504. }
  505. }
  506. }
  507. }
  508. for(int i = 0; i < nVertices; i++) {
  509. if(distance[i][i] < 0) {
  510. cout << "Negative cycle present" ;
  511. }
  512. }
  513. for (int i = 0; i < nVertices ; i++){
  514. for (int j = 0; j < nVertices; j++)
  515. {
  516. if(weightArray[i][j]==99999) cout<< "INF" <<" ";
  517. else
  518. cout<< weightArray[i][j]<<" ";
  519. }
  520. cout <<endl;
  521.  
  522. }
  523. for (int i = 0; i < nVertices ; i++){
  524. for (int j = 0; j < nVertices; j++)
  525. {
  526. if(distance[i][j]==99999) cout<< "INF" <<" ";
  527. else cout << distance[i][j]<<" ";
  528. }
  529. cout << endl;
  530.  
  531. }
  532. for (int i = 0; i < nVertices ; i++){
  533. for (int j = 0; j < nVertices; j++)
  534. {
  535. if(path[i][j]==-1) cout<< "INF" <<" ";
  536. else
  537. cout<< path[i][j]+1<<" ";
  538. }
  539. cout <<endl;
  540.  
  541. }
  542.  
  543. int i=0;
  544. int j=1;
  545. cout << "path between" << i+1 << " " << j+1<<endl;
  546. stack <int> Stack;
  547. Stack.push(j);
  548. cout << j+1<<" ";
  549. int a = i;
  550. int b = j;
  551. while (true) {
  552.  
  553. b = path[a][b];
  554. if(b == -1) {
  555. return;
  556. }
  557. Stack.push(b);
  558. cout << b+1<<" ";
  559. if(b == a) {
  560. break;
  561. }
  562.  
  563. }
  564. /// (u,v) edge thaktei hobe
  565. /// (p,q) er majhe notun path ber korte hobe jetate (u,v) thakbe
  566. int u=0;
  567. int v=2;
  568. int p=0;
  569. int q=3;
  570. // for (int i = 0; i < nVertices ; i++){
  571. // for (int j = 0; j < nVertices; j++){
  572. // distance[i][j] = weightArray[i][j];
  573. //
  574. // }
  575. //}
  576.  
  577. for(int k=0; k < nVertices; k++){
  578.  
  579. // if(distance[i][j] > distance[i][p] + distance[p][q]+distance[q][j]){
  580. distance[p][q] = distance[p][u] + weightArray[u][v]+distance[v][q];
  581. // path[i][j] = path[k][j];
  582.  
  583.  
  584. }
  585. cout << "dis: "<<distance[p][q];
  586. // for(int k=0; k < nVertices; k++){
  587. // if(distance[p][k] == 99999 || distance[k][q] == 99999) {
  588. // continue;
  589. // }
  590. // if(distance[p][q] > distance[p][u] +distance[u][v]+distance[v][q]){
  591. // distance[p][q] =distance[p][u] +distance[u][v]+distance[v][q];
  592. //
  593. // }
  594. // }
  595. // cout << "new dist: "<<distance[i][j];
  596.  
  597.  
  598. // while(!Stack.empty()){
  599. // int a = Stack.pop();
  600. // printf("%d ",a);
  601. // }
  602.  
  603.  
  604.  
  605.  
  606. }
  607. void Graph::printGraph()
  608. {
  609. printf("\nNumber of vertices: %d, Number of edges: %d\n", nVertices, nEdges);
  610. for(int i=0; i<nVertices; i++)
  611. {
  612. printf("%d:", i+1);
  613. for(int j=0; j<adjList[i].getLength(); j++)
  614. {
  615. printf(" %d", adjList[i].getItem(j)+1);
  616. }
  617. printf("\n");
  618. }
  619. }
  620.  
  621. Graph::~Graph()
  622. {
  623. //write your destructor here
  624. delete [] color;
  625. delete [] dist;
  626. delete [] parent;
  627. delete [] finishTime;
  628. delete [] discoveryTime;
  629. delete [] adjList;
  630. }
  631.  
  632.  
  633. //**********************Graph class ends here******************************
  634.  
  635.  
  636. //******main function to test your code*************************
  637. int main(void)
  638. {
  639. int n,m;
  640. ifstream in ("testcasefloyd.txt");
  641. Graph g(true);
  642. int u,v,w;
  643. // scanf("%d", &n);
  644. in >> n;
  645. in >> m;
  646. g.setnVertices(n);
  647. // g.setnEdges(m);
  648. for(int i=0; i<m; i++)
  649. {
  650. in >> u;
  651. in >> v;
  652. in >> w;
  653. if ( g.weightArray[u-1][v-1]==-1)
  654. {
  655. g.weightArray[u-1][v-1] = w;
  656. if(g.directed==false) g.weightArray[v-1][u-1] = w;
  657. g.addEdge(u-1,v-1);
  658. }
  659. else if(g.weightArray[u-1][v-1]>w)
  660. {
  661. g.weightArray[u-1][v-1] = w;
  662. if(g.directed==false) g.weightArray[v-1][u-1] = w;
  663. g.addEdge(u-1,v-1);
  664. }
  665. }
  666. g.FloydWarshallAlgo();
  667.  
  668. //g.printGraph();
  669.  
  670. // Graph MST =
  671.  
  672. return 0;
  673.  
  674. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement