Advertisement
Guest User

Untitled

a guest
Dec 8th, 2015
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.66 KB | None | 0 0
  1. ~~~~~~~~~~~~~~~Graph.cpp~~~~~~~~~~~~~~~~~~
  2.  
  3. #include <stdlib.h>
  4. #include "Graph.h"
  5. #include <float.h>
  6.  
  7. struct Vertex {
  8. AdjacencyList adjacency_list;
  9. double distance;
  10. int predecessor;
  11. };
  12.  
  13. void Graph_construct( Graph *object )
  14. {
  15. object->vertex_count = 0;
  16. object->vertices = NULL;
  17. }
  18.  
  19.  
  20. void Graph_add_vertex( Graph *object, int vertex_id )
  21. {
  22. object->vertex_count += 1;
  23. object->vertices[vertex_id];
  24. //add vertex to graph some how
  25. }
  26.  
  27.  
  28. void Graph_add_edge( Graph *object, int start_vertex, int end_vertex, double weight )
  29. {
  30. //make sure they exist first.
  31. //Shouldnt break anything if it exists.
  32. Graph_add_vertex(object, start_vertex);
  33. Graph_add_vertex(object, end_vertex);
  34.  
  35. //since they are all bi directional, add both vertexes to each others adjacency list somehow.
  36.  
  37. object->vertices[start_vertex].adjacency_list; //some other magic here to finish the code
  38.  
  39. //or maybe this but it has to be a pointer but its not
  40. object->vertices[start_vertex].adjacency_list->EdgeInfo->vertex_id=end_vertex;
  41.  
  42. //maybe this
  43. object->vertices[start_vertex].adjacency_list.count += 1;
  44. //someother stuff somehow
  45.  
  46. //then
  47. //also add the weight somewhere to the edge's info somehow, probably EdgeInfo from AdjacencyList. somehow
  48.  
  49. //should be done after that.
  50. }
  51.  
  52.  
  53. void Graph_destroy( Graph *object )
  54. {
  55. if( object->vertices != NULL ) {
  56. for( int i = 0; i < object->vertex_count; ++i ) {
  57. AdjacencyList_destroy( &object->vertices[i].adjacency_list );
  58. }
  59. free( object->vertices );
  60. }
  61. }
  62.  
  63.  
  64. int bellman_ford( Graph *object, int start_vertex )
  65. {
  66. // Step 1: initialize graph
  67. for (int i = 0; i < object->vertex_count; ++i) {
  68. object->vertices[i].distance = DBL_MAX;
  69. object->vertices[i].predecessor = -1; // -1 is no predecessor
  70. }
  71. object->vertices[start_vertex].distance = 0.0;
  72.  
  73. // Step 2: relax edges repeatedly //44.40
  74. for (int i = 0; i < object->vertex_count; ++i) {
  75. //for each edge figure out weight
  76. //if new weight cost < current weight cost
  77. //current weight cost = new weight cost
  78. //predecessor = the cheaper vertex from the new weight cost
  79. }
  80. // Step 3: check for negative-weight cycles
  81. //code for that; something if
  82.  
  83. return 0;
  84.  
  85.  
  86. }
  87.  
  88.  
  89. void print_path_to( Graph *object, int destination_vertex )
  90. {
  91.  
  92. }
  93. ~~~~~~~~~~~~Graph.h~~~~~~~~~~~~~~~~~
  94.  
  95. #ifndef GRAPH_H
  96. #define GRAPH_H
  97.  
  98. #include "AdjacencyList.h"
  99.  
  100. struct Vertex;
  101.  
  102. typedef struct {
  103. int vertex_count;
  104. struct Vertex *vertices;
  105. } Graph;
  106.  
  107. void Graph_construct( Graph *object );
  108. void Graph_add_vertex( Graph *object, int vertex_id );
  109. void Graph_add_edge( Graph *object, int start_vertex, int end_vertex, double weight );
  110. void Graph_destroy( Graph *object );
  111.  
  112. // Executes the Bellman-Ford algorithm on the graph using the given start vertex to find the
  113. // shortest path to all other vertices. This function mutates the graph object by updating
  114. // distance and predecessor information on the vertices of the graph. Returns true if the
  115. // algorithm is successful, false if the graph contains a negative weight cycle reachable from
  116. // the start vertex.
  117. //
  118. int bellman_ford( Graph *object, int start_vertex );
  119.  
  120. // Outputs to stdout a representation of the path from to the destination vertex starting from
  121. // the previously supplied start vertex. This function can only be used after a successful call
  122. // to bellman_ford. The precise format of the output is unspecified.
  123. //
  124. void print_path_to( Graph *object, int destination_vertex );
  125.  
  126. #endif
  127.  
  128. ~~~~~~~~~~~~~~AdjacencyList.cpp~~~~~~~~~~~~~~~~
  129.  
  130. #include <assert.h>
  131. #include <stdlib.h>
  132. #include "AdjacencyList.h"
  133.  
  134. struct AdjacencyListNode {
  135. EdgeInfo data;
  136. struct AdjacencyListNode *next;
  137. };
  138.  
  139.  
  140. void AdjacencyList_construct( AdjacencyList *object )
  141. {
  142. assert( object != NULL );
  143.  
  144. object->first = NULL;
  145. object->last = NULL;
  146. object->count = 0;
  147. }
  148.  
  149.  
  150. void AdjacencyList_destroy( AdjacencyList *object )
  151. {
  152. assert( object != NULL );
  153.  
  154. // NOTE: The ability to declare variables after executable statements is a C99 (or C++) feature.
  155. struct AdjacencyListNode *current = object->first;
  156. while( current != NULL ) {
  157. struct AdjacencyListNode *temp = current->next;
  158.  
  159. // NOTE: This assumes EdgeInfo has no special destruction needs.
  160. free( current );
  161. current = temp;
  162. }
  163.  
  164. // Put the SingleList object into a well defined state.
  165. AdjacencyList_construct( object );
  166. }
  167.  
  168.  
  169. void AdjacencyList_copy( AdjacencyList *destination, const AdjacencyList *source )
  170. {
  171. assert( destination != NULL && source != NULL );
  172.  
  173. AdjacencyList_destroy( destination );
  174.  
  175. AdjacencyListNode *current = source->first;
  176. while( current != NULL ) {
  177. AdjacencyList_push_back( destination, &current->data );
  178. current = current->next;
  179. }
  180. }
  181.  
  182.  
  183. size_t AdjacencyList_size( const AdjacencyList *object )
  184. {
  185. assert( object != NULL );
  186.  
  187. return object->count;
  188. }
  189.  
  190.  
  191. EdgeInfo AdjacencyList_front( const AdjacencyList *object )
  192. {
  193. assert( object != NULL && object->first != NULL );
  194.  
  195. return object->first->data;
  196. }
  197.  
  198.  
  199. EdgeInfo AdjacencyList_back( const AdjacencyList *object )
  200. {
  201. assert( object != NULL && object->last != NULL );
  202.  
  203. return object->last->data;
  204. }
  205.  
  206.  
  207. int AdjacencyList_equal( const AdjacencyList *object_1, const AdjacencyList *object_2 )
  208. {
  209. assert( object_1 != NULL && object_2 != NULL );
  210.  
  211. if( object_1->count != object_2->count ) return 0;
  212.  
  213. // The counts are equal...
  214. AdjacencyListNode *current_1 = object_1->first;
  215. AdjacencyListNode *current_2 = object_2->first;
  216.  
  217. while( current_1 != NULL ) {
  218. // NOTE: Too bad EdgeInfo can't be compared with !=.
  219. if( current_1->data.vertex_id != current_2->data.vertex_id ||
  220. current_1->data.weight != current_2->data.weight) return 0;
  221. current_1 = current_1->next;
  222. current_2 = current_2->next;
  223. }
  224. return 1;
  225. }
  226.  
  227.  
  228. void AdjacencyList_push_front( AdjacencyList *object, const EdgeInfo *item )
  229. {
  230. assert( object != NULL && item != NULL );
  231.  
  232. struct AdjacencyListNode *new_node =
  233. ( struct AdjacencyListNode * )malloc( sizeof( struct AdjacencyListNode ) );
  234.  
  235. // TODO: Maybe return an error indication if the allocation fails?
  236. if( new_node == NULL ) return;
  237.  
  238. // NOTE: This assumes EdgeInfo can be copied with assignment.
  239. new_node->data = *item;
  240. new_node->next = object->first;
  241. object->first = new_node;
  242.  
  243. // If the list was empty, it must be handled in a special way.
  244. if( object->last == NULL ) {
  245. object->last = object->first;
  246. }
  247. object->count++;
  248. }
  249.  
  250.  
  251. EdgeInfo AdjacencyList_pop_front( AdjacencyList *object )
  252. {
  253. assert( object != NULL && object->first != NULL );
  254.  
  255. // NOTE: This assumes EdgeInfo can be initialized with assignment.
  256. EdgeInfo temp_item = object->first->data;
  257. struct AdjacencyListNode *temp = object->first;
  258. object->first = object->first->next;
  259.  
  260. // NOTE: This assumes EdgeInfo has no special destruction needs.
  261. free( temp );
  262. object->count--;
  263. return temp_item;
  264. }
  265.  
  266.  
  267. void AdjacencyList_push_back( AdjacencyList *object, const EdgeInfo *item )
  268. {
  269. assert( object != NULL && item != NULL );
  270.  
  271. struct AdjacencyListNode *new_node =
  272. ( struct AdjacencyListNode * )malloc( sizeof( struct AdjacencyListNode ) );
  273.  
  274. // TODO: Maybe return an error indication if the allocation fails?
  275. if( new_node == NULL ) return;
  276.  
  277. // NOTE: This assumes EdgeInfo can be copied with assignment.
  278. new_node->data = *item;
  279. new_node->next = NULL;
  280.  
  281. if( object->count == 0 ) {
  282. object->first = new_node;
  283. }
  284. else {
  285. object->last->next = new_node;
  286. }
  287. object->last = new_node;
  288. object->count++;
  289. }
  290.  
  291.  
  292. AdjacencyListIterator AdjacencyList_begin( AdjacencyList *object )
  293. {
  294. assert( object != NULL );
  295.  
  296. AdjacencyListIterator result;
  297. result.object = object;
  298. result.current = object->first;
  299. return result;
  300. }
  301.  
  302.  
  303. AdjacencyListIterator AdjacencyList_end( AdjacencyList *object )
  304. {
  305. assert( object != NULL );
  306.  
  307. AdjacencyListIterator result;
  308. result.object = object;
  309. result.current = NULL;
  310. return result;
  311. }
  312.  
  313.  
  314. int AdjacencyList_equal_iterator( AdjacencyListIterator it_1, AdjacencyListIterator it_2 )
  315. {
  316. // We really want to say the iterators are valid. This doesn't completely do that.
  317. assert( it_1.object != NULL && it_2.object != NULL );
  318.  
  319. return (it_1.object == it_2.object) && (it_1.current == it_2.current);
  320. }
  321.  
  322.  
  323. AdjacencyListIterator AdjacencyList_next( AdjacencyListIterator it )
  324. {
  325. // We'd also like to say that it.current points into the list controlled by object.
  326. assert( it.object != NULL && it.current != NULL );
  327.  
  328. AdjacencyListIterator result;
  329. result.object = it.object;
  330. result.current = it.current->next;
  331. return result;
  332. }
  333.  
  334.  
  335. EdgeInfo *AdjacencyList_get( AdjacencyListIterator it )
  336. {
  337. assert( it.object != NULL && it.current != NULL );
  338. return &it.current->data;
  339. }
  340.  
  341.  
  342. void AdjacencyList_insert_after( AdjacencyListIterator it, const EdgeInfo *item )
  343. {
  344. assert( it.object != NULL && it.current != NULL && item != NULL );
  345. struct AdjacencyListNode *new_node =
  346. ( struct AdjacencyListNode * )malloc( sizeof( struct AdjacencyListNode ) );
  347.  
  348. // TODO: Maybe return an error indication if the allocation fails?
  349. if( new_node == NULL ) return;
  350.  
  351. // This assumes EdgeInfo can be copied with assignment.
  352. new_node->data = *item;
  353. new_node->next = it.current->next;
  354.  
  355. it.current->next = new_node;
  356. it.object->count++;
  357.  
  358. // If we are inserting after the last node (appending), we need to update the last pointer.
  359. if( it.current == it.object->last ) {
  360. it.object->last = new_node;
  361. }
  362. }
  363.  
  364. ~~~~~~~~~~~~~~~~~~~~~~AdjacencyList.h~~~~~~~~~~~~~~~~~~~~
  365.  
  366. #ifndef ADJACENCYLIST_H
  367. #define ADJACENCYLIST_H
  368.  
  369. #include <stddef.h>
  370.  
  371. struct EdgeInfo {
  372. int vertex_id; // The edge "points" at this vertex.
  373. double weight; // The weight on the edge.
  374. };
  375.  
  376. typedef struct EdgeInfo EdgeInfo;
  377.  
  378. struct AdjacencyListNode;
  379.  
  380. typedef struct {
  381. struct AdjacencyListNode *first;
  382. struct AdjacencyListNode *last;
  383. size_t count;
  384. } AdjacencyList;
  385.  
  386. typedef struct {
  387. AdjacencyList *object;
  388. struct AdjacencyListNode *current;
  389. } AdjacencyListIterator;
  390.  
  391. // Lifecycle operations...
  392. void AdjacencyList_construct( AdjacencyList *object );
  393. void AdjacencyList_destroy( AdjacencyList *object );
  394. void AdjacencyList_copy( AdjacencyList *destination, const AdjacencyList *source );
  395.  
  396. // Non-modifying list operations...
  397. size_t AdjacencyList_size( const AdjacencyList *object );
  398. EdgeInfo AdjacencyList_front( const AdjacencyList *object );
  399. EdgeInfo AdjacencyList_back( const AdjacencyList *object );
  400. int AdjacencyList_equal( const AdjacencyList *object_1, const AdjacencyList *object_2 );
  401.  
  402. // Modifying list operations...
  403. void AdjacencyList_push_front( AdjacencyList *object, const EdgeInfo *item );
  404. EdgeInfo AdjacencyList_pop_front( AdjacencyList *object );
  405. void AdjacencyList_push_back( AdjacencyList *object, const EdgeInfo *item );
  406.  
  407. // Iterator operations...
  408. AdjacencyListIterator AdjacencyList_begin( AdjacencyList *object );
  409. AdjacencyListIterator AdjacencyList_end( AdjacencyList *object );
  410. int AdjacencyList_equal_iterator( AdjacencyListIterator it_1, AdjacencyListIterator it_2 );
  411. AdjacencyListIterator AdjacencyList_next( AdjacencyListIterator it );
  412. EdgeInfo *AdjacencyList_get( AdjacencyListIterator it );
  413. void AdjacencyList_insert_after( AdjacencyListIterator it, const EdgeInfo *item );
  414.  
  415. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement