
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Optimize Wire Length in Electrical Circuit
This is a C++ Program to optimize Wire Length in Electrical Circuit.
Algorithm
Begin Function optimizeLength() : 1) Declare a array dist[N]. 2) sptSet[i] will be true if component i is included in shortest path tree or shortest distance from src to i is finalized. 3) Initialize all distances as INFINITE and stpSet[] as false 4) Distance of source component from itself will be always 0. 5) Run a for loop cnt = 0 to N-2, Find shortest path for all components. A) Pick the minimum distance component from the set of components not yet processed. B) Mark the picked component as processed. C) Update dist value of the adjacent components of the picked component. D) Update dist[v] only if is not in sptSet, there is an edge from u to v, and total weight of path from src to v through u is smaller than current value of dist[v]. End
Example
#include <limits.h> #include <iostream> using namespace std; #define N 6 int minDist(int dist[], bool sptSet[]) { //to find component with minimum distance value. int min = INT_MAX, min_index; for (int v = 0; v < N; v++) if (sptSet[v] == false && dist[v] <= min) min = dist[v], min_index = v; return min_index; } void displaySolution(int dist[], int n) { // display the solution. cout << "Component\tDistance from other component\n"; for (int i = 0; i < n; i++) printf("%d\t\t%d\n", i, dist[i]); } void optimizeLength(int g[N][N], int src) { //perform optimizeLength() function int dist[N]; bool sptSet[N]; for (int i = 0; i < N; i++) dist[i] = INT_MAX, sptSet[i] = false; dist[src] = 0; //Find shortest path for all components. for (int cnt = 0; cnt < N - 1; cnt++) { //Pick the minimum distance component from the set of //components not yet processed. int u = minDist(dist, sptSet); //Mark the picked component as processed. sptSet[u] = true; //Update dist value of the adjacent components of the picked component. for (int v = 0; v < N; v++) if (!sptSet[v] && g[u][v] && dist[u] != INT_MAX && dist[u] + g[u][v] < dist[v]) //Update dist[v] only if is not in sptSet, there is an edge from //u to v, and total weight of path from src to v through u is //smaller than current value of dist[v]. dist[v] = dist[u] + g[u][v]; } displaySolution(dist, N); } int main() { int g[N][N] = { { 0, 0, 6, 7, 0, 4}, { 4, 0, 8, 0, 1, 2 }, {0, 9, 0, 2,0, 4 },{ 0, 0, 7, 0, 9, 5 }, { 0, 1, 0, 0, 6,7 }, { 6, 7, 0, 0, 2,3} }; cout << "Enter the starting component: "; int s; cin >> s; optimizeLength(g, s); return 0; }
Output
Enter the starting component: 4 Component Distance from other component 0 5 1 1 2 9 3 11 4 0 5 3
- Related Articles
- Electrical Circuit Devices
- Which has less electrical resistance: a thin wire or a thick wire (of the same length and same material)?
- Why does one remove the plastic coating of the electrical wire from the ends, before connecting it to the electric circuit?
- How does fuse wire protect electrical appliances?
- How does use of a fuse wire protect electrical appliances?
- When does a wire in an electric circuit generate a magnetic field?
- In which wire in a A.C. housing circuit is the switch introduced to operate the lights?
- What is DC Voltage? – Definition, Circuit Symbol, and Wire Color Codes
- Give two reasons why different electrical appliances in a domestics circuit are connected in parallel.
- Why does a nichrome wire get heated in an electric circuit whereas nichrome does?
- Choose the true and false statements from the following:1. It is convenient to represent electric components by symbols. 2. A connecting wire is symbolized by a zig-zag line in the circuit diagram.3. When an electric current flows through a wire, the wire gets heated.4. The key or switch can be placed anywhere in the circuit.5. The amount of heat produced in a wire depends on its material, length and thickness.6. CFLs consume more electricity than ordinary bulbs.7. For different requirements, the wire of different materials, different length and thicknesses are used.8. A fuse is used to save energy in electrical circuits.9. MCBs are the switches which automatically turn off when current in a circuit exceeds the safe limit.10. When an electric current flows through a wire, it behaves like a magnet.
- If the length of a wire is doubled by taking more of the wire, What happens to its resistance?
- Explain the importance of using in a household electric circuit (i) fuse, and (ii) earthing wire.
- $2.2$ cubic dm of brass is to be drawn into a cylindrical wire $0.25\ cm$ in diameter. Find the length of the wire.
- How to optimize nested if...elif...else in Python?

Advertisements