The most effective way to find circular links in a list

Given the following redirect list

[
    {
        "old": "a",
        "target": "b"
    },
    {
        "old": "b",
        "target": "c"
    },
    {
        "old": "c",
        "target": "d"
    },
    {
        "old": "d",
        "target": "a"
    },
    {
        "old": "o",
        "target": "n"
    },
    {
        "old": "n",
        "target": "b"
    },
    {
        "old": "j",
        "target": "x"
    },
    {
        "old": "whatever",
        "target": "something"
    }
]

Here we see that the first element "a" should be redirected to "b". If we go to the list, we will see the following template:

a -> b
b -> c
c -> d
d -> a

So, we get a circular link, since “a” will eventually point to “d” and “d” will point to “a”.

What would be the most effective way to find circular links?

I came up with the following algorithm in C #

var items = JsonConvert.DeserializeObject<IEnumerable<Item>>(json)
    .GroupBy(x => x.Old)
    .Select(x => x.First())
    .ToDictionary(x => x.Old, x => x.Target);
var circulars = new Dictionary<string, string>();
foreach (var item in items)
{
    var target = item.Value;
    while (items.ContainsKey(target))
    {
        target = items[target];

        if (target.Equals(item.Key))
        {
            circulars.Add(target, item.Value);
            break;
        }
    }
}

This will give me a list containing 4 items that look like this:

[
    {
        "old": "a",
        "target": "b"
    },
    {
        "old": "b",
        "target": "c"
    },
    {
        "old": "c",
        "target": "d"
    },
    {
        "old": "d",
        "target": "a"
    }
]

But they are only interested in telling the user something like

", , -" a " " b ", " c ", " d ", " a "

, - ? , () ...:)

+4
1

, - " - , ". , node , , . , DFS- , , DFS .

node , node:

/// <summary>
/// Returns a node that is part of a cycle or null if no cycle is found
/// </summary>
static string FindCycleHelper(string start, Dictionary<string, string> successors, HashSet<string> stackVisited)
{
    string current = start;
    while (current != null)
    {
        if (stackVisited.Contains(current))
        {
            // this node is part of a cycle
            return current;
        }

        stackVisited.Add(current);

        successors.TryGetValue(current, out current);
    }

    return null;
}

, , node ( previouslyVisited):

/// <summary>
/// Returns a node that is part of a cycle or null if no cycle is found
/// </summary>
static string FindCycleHelper(string start, Dictionary<string, string> successors, HashSet<string> stackVisited, HashSet<string> previouslyVisited)
{
    string current = start;
    while (current != null)
    {
        if (previouslyVisited.Contains(current))
        {
            return null;
        }
        if (stackVisited.Contains(current))
        {
            // this node is part of a cycle
            return current;
        }

        stackVisited.Add(current);

        successors.TryGetValue(current, out current);
    }

    return null;
}

static string FindCycle(string start, Dictionary<string, string> successors, HashSet<string> globalVisited)
{
    HashSet<string> stackVisited = new HashSet<string>();

    var result = FindCycleHelper(start, successors, stackVisited, globalVisited);

    // update collection of previously processed nodes
    globalVisited.UnionWith(stackVisited);

    return result;
}

old node . , node, :

// static testdata - can be obtained from JSON for real code
IEnumerable<Item> items = new Item[]
{
    new Item{ Old = "a", Target = "b" },
    new Item{ Old = "b", Target = "c" },
    new Item{ Old = "c", Target = "d" },
    new Item{ Old = "d", Target = "a" },
    new Item{ Old = "j", Target = "x" },
    new Item{ Old = "w", Target = "s" },
};
var successors = items.ToDictionary(x => x.Old, x => x.Target);

var visited = new HashSet<string>();

List<List<string>> cycles = new List<List<string>>();

foreach (var item in items)
{
    string cycleStart = FindCycle(item.Old, successors, visited);

    if (cycleStart != null)
    {
        // cycle found, get detail information about involved nodes
        List<string> cycle = GetCycleMembers(cycleStart, successors);
        cycles.Add(cycle);
    }
}

.

foreach (var cycle in cycles)
{
    Console.WriteLine("Cycle:");
    Console.WriteLine(string.Join(" # ", cycle));
    Console.WriteLine();
}

GetCycleMembers - node:

/// <summary>
/// Returns the list of nodes that are involved in a cycle
/// </summary>
/// <param name="cycleStart">This is required to belong to a cycle, otherwise an exception will be thrown</param>
/// <param name="successors"></param>
/// <returns></returns>
private static List<string> GetCycleMembers(string cycleStart, Dictionary<string, string> successors)
{
    var visited = new HashSet<string>();
    var members = new List<string>();
    var current = cycleStart;
    while (!visited.Contains(current))
    {
        members.Add(current);
        visited.Add(current);
        current = successors[current];
    }
    return members;
}
+2

Source: https://habr.com/ru/post/1686618/


All Articles