Advertisement
pleasedontcode

"Vulnerability Edge" rev_01

Dec 1st, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Vulnerability Edge"
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2024-12-01 10:05:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Let G = (V, E) be a weighted, undirected, acyclic, */
  21.     /* connected graph with weight function w : V → Z. */
  22.     /* Let  n be the number of vertices in G. Observe */
  23.     /* that such a graph has exactly n − 1 edges. We are */
  24.     /* also given  a cost c : E → Z per edge. Since G is */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Arduino.h> // Include Arduino library for basic functions
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. // Define constants and global variables
  37. #define INF 1000000 // Define a constant for infinity
  38. const int MAX_VERTICES = 100; // Define maximum number of vertices
  39. int weights[MAX_VERTICES]; // Array to hold weights of vertices
  40. int edgeCount = 0; // Counter for edges
  41. struct Edge {
  42.     int u, v, cost;
  43. } edges[MAX_VERTICES]; // Array to hold edges
  44. int visited[MAX_VERTICES]; // Array to track visited vertices
  45. // Assuming Node structure is defined elsewhere
  46. struct Node {
  47.     int vertex;
  48.     Node* next;
  49. };
  50. Node* adjList[MAX_VERTICES]; // Adjacency list for graph
  51.  
  52. // Depth First Search function
  53. int dfs(int vertex) {
  54.     visited[vertex] = 1;
  55.     int sum = weights[vertex];
  56.     Node *temp = adjList[vertex];
  57.     while (temp) {
  58.         if (!visited[temp->vertex]) {
  59.             sum += dfs(temp->vertex);
  60.         }
  61.         temp = temp->next;
  62.     }
  63.     return sum;
  64. }
  65.  
  66. // Find the edge with the smallest vulnerability
  67. void findMinVulnerability() {
  68.     int minVulnerability = INF, minEdgeIndex = -1;
  69.  
  70.     for (int i = 0; i < edgeCount; i++) {
  71.         int u = edges[i].u;
  72.         int v = edges[i].v;
  73.         int cost = edges[i].cost;
  74.  
  75.         // Temporarily remove the edge
  76.         memset(visited, 0, sizeof(visited));
  77.         visited[v] = 1; // Prevent DFS to v from u
  78.         int sumH1 = dfs(u);
  79.  
  80.         memset(visited, 0, sizeof(visited));
  81.         visited[u] = 1; // Prevent DFS to u from v
  82.         int sumH2 = dfs(v);
  83.  
  84.         // Restore edge and compute vulnerability
  85.         int vulnerability = cost - abs(sumH1 - sumH2);
  86.         if (vulnerability < minVulnerability) {
  87.             minVulnerability = vulnerability;
  88.             minEdgeIndex = i;
  89.         }
  90.     }
  91.  
  92.     // Use Serial.print instead of printf
  93.     Serial.print("Edge with the smallest vulnerability: ");
  94.     Serial.print(edges[minEdgeIndex].u);
  95.     Serial.print(" ");
  96.     Serial.println(edges[minEdgeIndex].v);
  97. }
  98.  
  99. void addEdge(int u, int v, int cost) {
  100.     // Add edge to the adjacency list
  101.     Node* newNode = new Node();
  102.     newNode->vertex = v;
  103.     newNode->next = adjList[u];
  104.     adjList[u] = newNode;
  105.  
  106.     // Increment edge count
  107.     edgeCount++;
  108. }
  109.  
  110. void setup(void)
  111. {
  112.     // Initialize Serial communication
  113.     Serial.begin(115200);
  114.    
  115.     // put your setup code here, to run once:
  116.     Serial.println("Enter the number of vertices: ");
  117.     while (Serial.available() == 0) {} // Wait for input
  118.     int n = Serial.parseInt(); // Read number of vertices
  119.  
  120.     Serial.println("Enter the weights of vertices:");
  121.     for (int i = 1; i <= n; i++) {
  122.         while (Serial.available() == 0) {} // Wait for input
  123.         weights[i] = Serial.parseInt(); // Read weights
  124.     }
  125.  
  126.     Serial.println("Enter the edges and their costs (u v cost):");
  127.     while (true) {
  128.         int u, v, cost;
  129.         if (Serial.available() > 0) {
  130.             u = Serial.parseInt();
  131.             v = Serial.parseInt();
  132.             cost = Serial.parseInt();
  133.             if (u == 0 && v == 0 && cost == 0) break; // Use a break condition
  134.         }
  135.  
  136.         addEdge(u, v, cost);
  137.         addEdge(v, u, cost); // For undirected graph
  138.  
  139.         edges[edgeCount].u = u;
  140.         edges[edgeCount].v = v;
  141.         edges[edgeCount].cost = cost;
  142.         edgeCount++;
  143.     }
  144.  
  145.     findMinVulnerability();
  146. }
  147.  
  148. void loop(void)
  149. {
  150.     // put your main code here, to run repeatedly:
  151. }
  152.  
  153. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement