C
C#5mo ago
Tsjech

Recursive method and memory problems

I am implementing the DFS algorithm and I am keeping track of what routes have been passed and their value, the core of the algorithm looks like this:
private void DFSVisitting(Planet current, Planet old, bool[] visited, int len)
{
int length;
length = len;
visited[current.id] = true;
if (!(current == old)) {
foreach (Route route in gameScreen.board.routes) {
if (!(route.color == color && route.built)) continue;
if ((route.planet1 == current && route.planet2 == old) || (route.planet2 == current && route.planet1 == old)) length += route.costs;
}
}

List<Planet> neighbours = adjacent[current.id];
foreach (Planet p in neighbours) {
if (!visited[p.id]) {
DFSVisitting(p, current, visited, length);
// returns here
lengths.Add(length);
length = 0;
}
}
lengths.Add(length);
}
private void DFSVisitting(Planet current, Planet old, bool[] visited, int len)
{
int length;
length = len;
visited[current.id] = true;
if (!(current == old)) {
foreach (Route route in gameScreen.board.routes) {
if (!(route.color == color && route.built)) continue;
if ((route.planet1 == current && route.planet2 == old) || (route.planet2 == current && route.planet1 == old)) length += route.costs;
}
}

List<Planet> neighbours = adjacent[current.id];
foreach (Planet p in neighbours) {
if (!visited[p.id]) {
DFSVisitting(p, current, visited, length);
// returns here
lengths.Add(length);
length = 0;
}
}
lengths.Add(length);
}
the problem is, if the network is searching down a path and ends in a dead at the function returns at the place of my comment, but the length variable has been forgotten by the program (atleast what i think) because this method has been called multiple times already and is now returning to an older instance of itself. So is there any way to surpass this issue? Nodes are planets and Edges are Routes.
5 Replies
Tsjech
Tsjech5mo ago
@TeBeCo
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Tsjech
Tsjech5mo ago
hmm returning a value wouldnt be handy, because it generally needs to forget the when it finds a dead end and grab the one it was working on before that branch. but thank you
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Tsjech
Tsjech5mo ago
I just fed the nodes ive been at to the method itself using its params, so i didnt have to return anything but it did keep track of old data when it popped
Want results from more Discord servers?
Add your server
More Posts
In my code, it says I have a null parameter, this is my first bit of code, Where am I going wrong?My first code ever. using Azure; using Azure.AI.OpenAI; using System; using static System.EnvironmeGraph API - HttpClient returns 404, but curl (and Graph Explorer) for same request works?I'm completely lost. I've resorted to just calling curl from C# as this is completely non-sensical tSFTP through a proxy serverI'm using SSH.NET and since I'm unable to connect to production servers directly, I have to first SSMerging 2 projects c# formsIm running into an issue with combining 2 projects in visual studio 2022. I need to combine those 2 Problem with a Vehicle CatalogueI am trying to solve a problem where i need to create something like a vehicle catalogue it works foIs there any way to *conditionally* set a property with EF `.ExecuteUpdateAsync()`?Right now, I'm doing ```cs var thing = await _ctx.Things.FirstOrDefaultAsync(t => t.Id == req.Id); iStupid question about Visual StudioHello everyone, I feel like I'm losing my mind with Microsoft Visual Studio. My college wants me to Can't withdraw and save new balance to databaseHey can someone help me with my issues I've been stuck on for hours? I am trying to make a withdraw ✅ RPC not available on trying to read a Excel workbook using interopI was trying to use `Microsoft.Office.Interop.Excel` to open a excel file and read its content. but ✅ Verify ReCaptcha on private serverI have implemented ReCaptcha that is deployed to private server which does not have access to intern