Skip to content

Commit 4265ff0

Browse files
committed
added Min Cost to Repair Edges
1 parent 140d8f1 commit 4265ff0

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Graph/MinCostToRepairEdges/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Minimum Cost to Repair Edges
2+
3+
[Leetcode Link](https://leetcode.com/discuss/interview-question/357310)
4+
5+
## Problem:
6+
7+
There's an undirected connected graph with n nodes labeled 1..n. But some of the edges has been broken disconnecting the graph. Find the minimum cost to repair the edges so that all the nodes are once again accessible from each other.
8+
9+
Input:
10+
11+
- `n`, an int representing the total number of nodes.
12+
- `edges`, a list of integer pair representing the nodes connected by an edge.
13+
- `edgesToRepair`, a list where each element is a triplet representing the pair of nodes between which an edge is currently broken and the cost of reparing that edge, respectively (e.g. [1, 2, 12] means to repear an edge between nodes 1 and 2, the cost would be 12).
14+
15+
## Example:
16+
17+
```
18+
Input: n = 5, edges = [[1, 2], [2, 3], [3, 4], [4, 5], [1, 5]], edgesToRepair = [[1, 2, 12], [3, 4, 30], [1, 5, 8]]
19+
Output: 20
20+
Explanation:
21+
There are 3 connected components due to broken edges: [1], [2, 3] and [4, 5].
22+
We can connect these components into a single component by repearing the edges between nodes 1 and 2, and nodes 1 and 5 at a minimum cost 12 + 8 = 20.
23+
```
24+
25+
```
26+
Input: n = 6, edges = [[1, 2], [2, 3], [4, 5], [3, 5], [1, 6], [2, 4]], edgesToRepair = [[1, 6, 410], [2, 4, 800]]
27+
Output: 410
28+
```
29+
30+
```
31+
Input: n = 6, edges = [[1, 2], [2, 3], [4, 5], [5, 6], [1, 5], [2, 4], [3, 4]], edgesToRepair = [[1, 5, 110], [2, 4, 84], [3, 4, 79]]
32+
Output: 79
33+
```
34+
35+
## Note:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import defaultdict
2+
3+
4+
def updateCost(neighbors, b, cost):
5+
for i in range(len(neighbors)):
6+
if neighbors[i][0] == b:
7+
neighbors[i][1] = cost
8+
9+
10+
def minCostRepair(n, edges, edgesToRepair):
11+
# create edge lists associated with cost
12+
edgeList = defaultdict(list)
13+
for a, b in edges:
14+
edgeList[a].append([b, 0])
15+
edgeList[b].append([a, 0])
16+
# print(edgeList)
17+
for a, b, cost in edgesToRepair:
18+
updateCost(edgeList[a], b, cost)
19+
updateCost(edgeList[b], a, cost)
20+
# print(edgeList)
21+
# find shorted spanning graph
22+
reached = set()
23+
totalCost = 0
24+
queue = list()
25+
queue.append([1, 0])
26+
while len(reached) < n:
27+
current = queue.pop(0)
28+
currentNode = current[0]
29+
cost = current[1]
30+
if currentNode not in reached:
31+
reached.add(currentNode)
32+
totalCost += cost
33+
queue.extend(edgeList[currentNode])
34+
queue.sort(key=lambda pair: pair[1])
35+
return totalCost
36+
37+
38+
n = 6
39+
edges = [[1, 2], [2, 3], [4, 5], [5, 6], [1, 5], [2, 4], [3, 4]]
40+
edgesToRepair = [[1, 5, 110], [2, 4, 84], [3, 4, 79]]
41+
print(minCostRepair(n, edges, edgesToRepair))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Languages used: Java and Python
7777
- [All Paths From Src To Target](Graph/AllPathsFromSrcToTarget)
7878
- [Course Schedule II](Graph/CourseSchedule2)
7979
- [Escape Large Maze](Graph/EscapeLargeMaze)
80+
- [Minimum Cost to Repair Edges](Graph/MinCostToRepairEdges)
8081
- [Number to Make Network Connected](Graph/NumToMakeNetworkConnected)
8182
- [Remove Duplicates from Sorted List](Array/RemoveDuplicatesFromSortedList)
8283
- [Reconstruct Itinerary](Graph/ReconstructItinerary)

0 commit comments

Comments
 (0)