-3

spsolve is then - sometimes - unable to find a solution.

Our teacher gave us test cases that we have to satisfy however I passed all of them but seems to fail the hidden test cases.

My code checks for the following: If they share a node and only those two resistors are connected then print SERIES else NEITHER. If their previous resistor is in series, then its is in SERIES (see test case no 2). If the resistor's ends are connected to the same nodes then print Parallel.

Can you suggest some inputs or any possible scenarios that a code wouldn't be able to answer correctly? or maybe a suggestion on what type of algorithm I should use for this problem

As I am performing nodal analysis, a singular matrix is expected since the position of the ground potential is generally not well-defined. However, before the update, a solution was found in 99% of the cases, maybe more. Now, I'm at 10% for large systems at best. I have not changed the algorithm and for a few tests, I have used identical code as before. Here is how I set up my calculation:

I generate a random three-dimensional network of resistors (I realize that I could accidentally create unsolvable networks but the percentages above should not change that drastically). The only SciPy/NumPy functions used here is np.random I create a sparse lil-matrix which I fill with conductance values extracted from my resistor network. I also create a solution vector which is not sparse. I convert the conductance matrix to csr-format and use the spsolve method. This is where my code lately fails.

  • 2
    There is no need to deface your question once you receive an answer. – Siguza Jun 03 '23 at 14:31
  • 1
    As above. Stack Overflow's purpose is to act as a question and answer repository for a wide readership, not just for the person posting the question. By deleting all, or part, of your question after people have taken the time to post lengthy answers is both disrespectful to the people who have tried to help you, and also makes the post far less valuable to anyone else who might find it helpful in the future. – David Buck Jun 03 '23 at 15:23

2 Answers2

1

If they share a node and only those two resistors are connected then print SERIES

This seems a good rule, but "only those ... are connected" is somewhat ambiguous. The condition is that one resistor in the given pair has the following property: all (forward) paths that start at this resistor pass via the other resistor.

However, your code does not implement this rule correctly, since it does not consider all paths, but just the first one it finds.

If the resistor's ends are connected to the same nodes then print Parallel.

This seems the right rule for identifying a pair as connected in parallel.

However, your code does not implement this rule correctly, since it only requires that one node matches.

Can you suggest some inputs or any possible scenarios that my code wouldn't be able to answer correctly?

For this input your code wrongly identifies a pair as SERIES, while it should be NEITHER:

             ┌ R2 ─ b ─ R3 ┐
Vdd ─ R1 ─ a ┤             ├ GND
             └───── R4 ────┘

Encoded as

4 1
R1 Vdd a 1
R2 a   b 1
R3 b GND 1
R4 a GND 1
R1 R3

Your code will print:

SERIES

For this second scenario, your code has mixed answers for the possible pairings with R1:

    ┌───────── R1 ─────────┐
Vdd ┤                      ├ GND
    └ R2 ─ a ─ R3 ─ b ─ R4 ┘

Encoded as:

4 3
R1 Vdd GND 1
R2 Vdd a   1
R3 a   b   1
R4 b   GND 1
R1 R2
R1 R3
R1 R4

Your code will print:

PARALLEL
NEITHER
PARALLEL

or maybe a suggestion on what type of algorithm i should use for this problem

The check for PARALLEL should have an and operator, and traverse_series should be replaced with a function that gets both resistors as arguments and which returns whether or not it is possible to find a path from the first resistor to a leaf, without passing via the second resistor.

Not a problem, but I would try to avoid searching the resitors list over and over again for a certain resistor. It will be more useful to have a dictionary for that. I would also create an adjacency list.

Here is an implementation (spoiler):

from collections import defaultdict def parse_input(): N, Q = map(int, input().split()) resistors = [] for _ in range(N): name, node1, node2, resistance = input().split() resistance = int(resistance) resistors.append((name, node1, node2, resistance)) queries = [input().split() for _ in range(Q)] return resistors, queries class Graph: def init(self, resistors): self.edges = { name: (start, end) for name, start, end, _ in resistors } self.adj = defaultdict(list) for name, start, end, _ in resistors: self.adj[start].append(name) def can_bypass(self, r1, r2): successors = self.adj[self.edges[r1][1]] if not successors: # found a path that bypasses r2! return True return any(successor != r2 and self.can_bypass(successor, r2) for successor in successors) def are_in_series(self, r1, r2): return not (self.can_bypass(r1, r2) and self.can_bypass(r1, r2)) def are_in_parallel(self, r1, r2): return self.edges[r1] == self.edges[r2] resistors, nodes, queries = parse_input() graph = Graph(resistors) for r1, r2 in queries: if graph.are_in_parallel(r1, r2): print("PARALLEL") elif graph.are_in_series(r1, r2): print("SERIES") else: print("NEITHER")

trincot
  • 317,000
  • 35
  • 244
  • 286
-1

Electron travel between Vdd and ground. The Dijkstra algorithm ( google it ) finds these routes. If two resisters are on the same route, they are in series.

Setup

  • LOOP over every pair of resisters
    • IF the ends of the two resistor are connected to the same nodes - mark as parallel
  • Create adjacency list for resisters, combining parallels together into one edge.

To check if R1 and R2 are in series:

  • IF R1 and R2 are marked parallel return PARALLEL
  • Use Dijkstra to find all routes from Vdd to R1
  • IF no route return NO
  • Use Dijkstra to find all route from R1 to GND
  • IF no route return NO
  • IF R2 itself, or as part of a parallel, on any routes found return SERIES

Note that this will return YES for R3 and R4 in sample input 1. I do not understand why R3 and R4 are not in series - electricity will flow through R3 and then R4 to reach ground.

Obviously, I do not understand your definition of "in series". Please provide this definition. In particular:

R1 Vdd a
R2 a b
R3 a b
R4 b GND

Are R1 and R4 in series or not?

ravenspoint
  • 19,093
  • 6
  • 57
  • 103