C++ Program to Optimize Wire Length in Electrical Circuit

This is a C++ Program to optimize wire length in electix circuit. This problem can be reduced to finding the shortest path between two components.

Here is source code of the C++ Program to Optimize Wire Length in Electrical Circuit. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. // Number of components in the graph
  8. #define V 9
  9.  
  10. // A utility function to find the component with minimum distance value, from
  11. // the set of components not yet included in shortest path tree
  12. int minDistance(int dist[], bool sptSet[])
  13. {
  14.     // Initialize min value
  15.     int min = INT_MAX, min_index;
  16.  
  17.     for (int v = 0; v < V; v++)
  18.         if (sptSet[v] == false && dist[v] <= min)
  19.             min = dist[v], min_index = v;
  20.  
  21.     return min_index;
  22. }
  23.  
  24. // A utility function to print the constructed distance array
  25. void printSolution(int dist[], int n)
  26. {
  27.     cout << "Component\tDistance from other component\n";
  28.     for (int i = 0; i < V; i++)
  29.         printf("%d\t\t%d\n", i, dist[i]);
  30. }
  31.  
  32. // Funtion that implements Dijkstra's single source shortest path algorithm
  33. // for a graph represented using adjacency matrix representation
  34. void optimizeLength(int graph[V][V], int src)
  35. {
  36.     int dist[V]; // The output array.  dist[i] will hold the shortest
  37.     // distance from src to i
  38.  
  39.     bool sptSet[V]; // sptSet[i] will true if component i is included in shortest
  40.     // path tree or shortest distance from src to i is finalized
  41.  
  42.     // Initialize all distances as INFINITE and stpSet[] as false
  43.     for (int i = 0; i < V; i++)
  44.         dist[i] = INT_MAX, sptSet[i] = false;
  45.  
  46.     // Distance of source component from itself is always 0
  47.     dist[src] = 0;
  48.  
  49.     // Find shortest path for all components
  50.     for (int count = 0; count < V - 1; count++)
  51.     {
  52.         // Pick the minimum distance component from the set of components not
  53.         // yet processed. u is always equal to src in first iteration.
  54.         int u = minDistance(dist, sptSet);
  55.  
  56.         // Mark the picked component as processed
  57.         sptSet[u] = true;
  58.  
  59.         // Update dist value of the adjacent components of the picked component.
  60.         for (int v = 0; v < V; v++)
  61.  
  62.             // Update dist[v] only if is not in sptSet, there is an edge from
  63.             // u to v, and total weight of path from src to  v through u is
  64.             // smaller than current value of dist[v]
  65.             if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u]
  66.                     + graph[u][v] < dist[v])
  67.                 dist[v] = dist[u] + graph[u][v];
  68.     }
  69.  
  70.     // print the constructed distance array
  71.     printSolution(dist, V);
  72. }
  73.  
  74. // driver program to test above function
  75. int main()
  76. {
  77.     /* Let us create the example graph discussed above */
  78.     int graph[V][V] =
  79.             { { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, {
  80.                     0, 8, 0, 7, 0, 4, 0, 0, 2 },
  81.                     { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, { 0, 0, 0, 9, 0, 10, 0, 0,
  82.                             0 }, { 0, 0, 4, 0, 10, 0, 2, 0, 0 }, { 0, 0, 0, 14,
  83.                             0, 2, 0, 1, 6 }, { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, {
  84.                             0, 0, 2, 0, 0, 0, 6, 7, 0 } };
  85.  
  86.     cout << "Enter the starting component: ";
  87.     int s;
  88.     cin >> s;
  89.     optimizeLength(graph, s);
  90.  
  91.     return 0;
  92. }

Output:

$ g++ OptimizeWireLength.cpp
$ a.out
 
Enter the starting component: 1
Component	Distance from other component
0			4
1			0
2			8
3			15
4			22
5			12
6			12
7			11
8			10
 
Enter the starting component: 6
Component	Distance from other component
0			9
1			12
2			6
3			13
4			12
5			2
6			0
7			1
8			6

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.