Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. // Drake Lin
  2. // 5/20/19
  3. // Graphs
  4. // given adjacency matrix, calculate edge list and incidence matrix
  5.  
  6. #include "pch.h"
  7. #include <iostream>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. //create Graph struct with number of vertices and edges, initial adjacency matrix
  13. //initialize four matrices
  14. struct Graph {
  15.     int vertices;
  16.     int edges;
  17.  
  18.     //big matrix to account for big graphs
  19.     int adj[20][20];
  20. }one, two, three, four;
  21.  
  22. //prints adjacency matrix, use vertices to limit matrix to size
  23. //input: graph struct
  24. //output: none
  25. void printAdj(Graph g)
  26. {
  27.     cout << "Adjacency matrix: " << endl;
  28.     for (int i = 1; i < g.vertices+1; i++)
  29.     {
  30.         for (int j = 1; j < g.vertices+1; j++)
  31.         {
  32.             cout << g.adj[i][j];
  33.         }
  34.         cout << endl;
  35.     }
  36. }
  37.  
  38. //adding edge with vertices in matrixes
  39. //input: graph struct, vertices
  40. //output: none
  41. void addEdge(Graph &g, int v1, int v2)
  42. {
  43.     g.adj[v1][v2] = 1;
  44.     g.adj[v2][v1] = 1;
  45. }
  46.  
  47. //printing edge list
  48. //input: graph struct
  49. //output: none
  50. void printEdge(Graph g)
  51. {
  52.     cout << "Edges: " << endl;
  53.     for (int i = 0; i < 20; i++)
  54.     {
  55.         for (int j = i; j < 20; j++)
  56.         {
  57.             //detects if there is an edge in adjacency matrix
  58.             if (g.adj[i][j] == 1)
  59.             {
  60.                 cout << i << "," << j << endl;
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. //calculates and prints incidence matrix
  67. //input: graph struct
  68. //output: none
  69. void printInc(Graph &g)
  70. {
  71.     //initialize big matrix
  72.     cout << "Incidence Matrix: " << endl;
  73.     int inc[20][100];
  74.  
  75.     for (int i = 0; i < 20; i++)
  76.     {
  77.         for (int j = 0; j < 20; j++)
  78.         {
  79.             inc[i][j] = 0;
  80.         }
  81.     }
  82.  
  83.  
  84.     //initialize variable used to number edges
  85.     int vert = 0;
  86.  
  87.     for (int i = 0; i < 20; i++)
  88.     {
  89.         for (int j = i; j < 20; j++)
  90.         {
  91.             //see edges in adjacency matrix
  92.             if (g.adj[i][j] == 1)
  93.             {
  94.                 //print out
  95.                 inc[i][vert] = 1;
  96.                 inc[j][vert] = 1;
  97.  
  98.                 //move edge number
  99.                 vert++;
  100.             }
  101.         }
  102.     }
  103.  
  104.     //print out incidence matrix with size in mind
  105.     for (int i = 1; i < g.vertices+1; i++)
  106.     {
  107.         for (int j = 0; j < g.edges; j++)
  108.         {
  109.             cout << inc[i][j];
  110.         }
  111.         cout << endl;
  112.     }
  113. }
  114.  
  115. //Graph 1: p274, Figure 6.2.1
  116. //input: empty graph struct
  117. //output: none
  118. void create1(Graph &g)
  119. {
  120.     addEdge(g, 1, 2);
  121.     addEdge(g, 2, 3);
  122.     addEdge(g, 3, 4);
  123.     addEdge(g, 2, 4);
  124.     addEdge(g, 2, 5);
  125.     addEdge(g, 2, 6);
  126.     addEdge(g, 5, 6);
  127.     addEdge(g, 6, 7);
  128.     g.edges = 8;
  129.     g.vertices = 7;
  130. }
  131.  
  132. //Graph 2: p300, #1
  133. //input: empty graph struct
  134. //output: none
  135. void create2(Graph &g)
  136. {
  137.     addEdge(g, 1, 2);
  138.     addEdge(g, 2, 3);
  139.     addEdge(g, 1, 5);
  140.     addEdge(g, 3, 4);
  141.     addEdge(g, 1, 3);
  142.     addEdge(g, 1, 4);
  143.     addEdge(g, 5, 3);
  144.     addEdge(g, 5, 4);
  145.     g.edges = 8;
  146.     g.vertices = 5;
  147. }
  148.  
  149. //Graph 3: p300, #3
  150. //input: empty graph struct
  151. //output: none
  152. void create3(Graph &g)
  153. {
  154.     addEdge(g, 1, 2);
  155.     addEdge(g, 3, 5);
  156.     addEdge(g, 5, 4);
  157.     addEdge(g, 3, 4);
  158.     g.edges = 4;
  159.     g.vertices = 5;
  160. }
  161.  
  162. //Graph 4: p307, Fig. 6.7.2
  163. //input: empty graph struct
  164. //output: none
  165. void create4(Graph &g)
  166. {
  167.     addEdge(g, 1, 2);
  168.     addEdge(g, 1, 5);
  169.     addEdge(g, 1, 6);
  170.     addEdge(g, 2, 3);
  171.     addEdge(g, 3, 4);
  172.     addEdge(g, 2, 5);
  173.     addEdge(g, 4, 5);
  174.     addEdge(g, 4, 6);
  175.     g.edges = 8;
  176.     g.vertices = 6;
  177. }
  178.  
  179. //prints out adjacency matrix, edge list, incidence matrix
  180. //input: graph struct
  181. //output: none
  182. void printAll(Graph g)
  183. {
  184.     printAdj(g);
  185.     printEdge(g);
  186.     printInc(g);
  187. }
  188.  
  189. //main runs through all graphs
  190. int main()
  191. {
  192.     create1(one);
  193.     cout << "Graph 1:" << endl;
  194.     printAll(one);
  195.  
  196.     create2(two);
  197.     cout << endl << "Graph 2:" << endl;
  198.     printAll(two);
  199.  
  200.     create3(three);
  201.     cout << endl << "Graph 3:" << endl;
  202.     printAll(three);
  203.  
  204.     create4(four);
  205.     cout << endl << "Graph 4:" << endl;
  206.     printAll(four);
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement