Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #include "MatrixGraph.h"
  5.  
  6. /**
  7. * Initializeaza graful cu numarul de noduri primit ca parametru si aloca
  8. * memorie pentru matricea de adiacenta a grafului.
  9. */
  10. void init_matrix_graph(MatrixGraph *graph, int nodes) {
  11. /* TODO */
  12. graph->nodes = nodes;
  13. graph->matrix = (int **)calloc(nodes, sizeof(int *));
  14. if (graph->matrix == NULL) {
  15. fprintf(stderr, "Matrix not initialised.\n");
  16. return;
  17. }
  18. for (int i=0; i < nodes; ++i) {
  19. graph->matrix[i]=(int *) calloc (nodes, sizeof(int));
  20. if (graph->matrix[i] == NULL) {
  21. fprintf(stderr, "Matrix not initialised.\n");
  22. return;
  23. }
  24. }
  25. }
  26.  
  27. /* Adauga o muchie intre nodurile sursa si destinatie */
  28. void add_edge_matrix_graph(MatrixGraph *graph, int src, int dest) {
  29. /* TODO */
  30. graph->matrix[src][dest] = 1;
  31. graph->matrix[dest][src] = 1;
  32. }
  33.  
  34. /* Returneaza 1 daca exista muchie intre cele doua noduri, 0 in caz contrar */
  35. int has_edge_matrix_graph(MatrixGraph *graph, int src, int dest) {
  36. /* TODO */
  37. if(graph->matrix[src][dest] == 1 && graph->matrix[dest][src] == 1) {
  38. return 1;
  39. }
  40.  
  41. return 0;
  42. }
  43.  
  44. /* Elimina muchia dintre nodurile sursa si destinatie */
  45. void remove_edge_matrix_graph(MatrixGraph *graph, int src, int dest) {
  46. /* TODO */
  47. graph->matrix[src][dest] = 0;
  48. graph->matrix[dest][src] = 0;
  49. }
  50.  
  51. /* Elibereaza memoria folosita de matricea de adiacenta a grafului */
  52. void clear_matrix_graph(MatrixGraph *graph) {
  53. /* TODO */
  54. for (int i=0; i < graph->nodes; ++i) {
  55. free(graph->matrix[i]);
  56. }
  57. free(graph->matrix);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement