Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone ,
I want to ensure that when one subregion is connected to another via
linkedSubRegions, and that other subregion is connected to a third one, all three are concatenated into a single group.

for example:

"987" is connected to "Schwerverkehr".
"Schwerverkehr" is connected to "975".
Therefore, "975", "987", and "Schwerverkehr" should be concatenated into one group.

 const region1: Region = {
    key: 1,
    name: 'Nahverkehr',
    subRegions: [
        { key: 1, name: '975', linkedSubRegions: [{ key: 25, name: 'Schwerverkehr' }] },
        { key: 2, name: '987', linkedSubRegions: [{ key: 25, name: 'Schwerverkehr' }] },
        { key: 3, name: 'Aschaffenburg', linkedSubRegions: [] },
        { key: 4, name: 'Aschaffenburg-Land', linkedSubRegions: [{ key: 3, name: 'Aschaffenburg' }] },
        { key: 5, name: 'Marktheidenfeld', linkedSubRegions: [{ key: 4, name: 'Aschaffenburg-Land' }] },
        { key: 25, name: 'Schwerverkehr', linkedSubRegions: [] },
    ],
};


result should be :

subRegions: [
       { key: 1, name: '975,987,Schwerverkehr'},
       { key: 3, name: 'Aschaffenburg,Aschaffenburg-Land,Marktheidenfeld'}
   ],


What I have tried:

mergeConnectedSubregions(region: Region): SubRegion[] {
    const subregionsMap: Map<number, SubRegion> = new Map();
    const visited: Set<number> = new Set();

    // Build a map of subregions for easy access
    for (const subregion of region.subRegions!) {
        subregionsMap.set(subregion.key!, subregion);
    }

    const mergedSubregions: SubRegion[] = [];
            const connectedSubregions: SubRegion[] = [];

    // Perform DFS for each subregion to identify connected subregions
    for (const subregion of region.subRegions!) {
        if (!visited.has(subregion.key!)) {
            dfs(subregion);
            // Merge the connected subregions
            const mergedNames = connectedSubregions.map(sub => sub.name).join(',');
            const mergedSubregion: SubRegion = {
                key: -1,
                name: mergedNames
            };
            mergedSubregions.push(mergedSubregion);
        }
    }

    return mergedSubregions;

    function dfs(subregion: SubRegion) {
        if (visited.has(subregion.key!)) return;
        visited.add(subregion.key!);
        connectedSubregions.push(subregion);
        for (const model of subregion.linkedSubRegions!) {
            const connectedSubregion = subregionsMap.get(model.key!);
            if (connectedSubregion) {
                dfs(connectedSubregion);
            }
        }
    }
  }

result was :
[
    {
        "key": -1,
        "name": "975,Schwerverkehr",

    },
    {
        "key": -1,
        "name": "975,Schwerverkehr,987",
    },
    {
        "key": -1,
        "name": "975,Schwerverkehr,987,Aschaffenburg",
    },
    {
        "key": -1,
        "name": "975,Schwerverkehr,987,Aschaffenburg,Aschaffenburg-Land",
    },
    {
        "key": -1,
        "name": "975,Schwerverkehr,987,Aschaffenburg,Aschaffenburg-Land,Marktheidenfeld"
 
    }
]
Posted
Updated 12-Feb-24 4:57am
v2
Comments
Richard Deeming 12-Feb-24 10:47am    
So you've tried nothing then?
Member 12785541 12-Feb-24 10:49am    
i tried a lot of functions , but unfortunately without success
Richard Deeming 12-Feb-24 10:51am    
Based on your question, your "what I have tried" is just the input and expected output. There is no information on what you have tried or where you are stuck.

Without that, we can't know whether we're suggesting something you've already tried and rejected.
Member 12785541 12-Feb-24 11:00am    
I have updated the question

1 solution

Look at Tarjan's strongly connected components algorithm[^].

The issue with the recursive algorithm is that it can cause a stack overflow on large graphs. An iterative algorithm also exists, which doesn't have this problem.
 
Share this answer
 
Comments
Member 12785541 14-Feb-24 5:11am    
Thanks ,that's was helpful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900