Techno Blender
Digitally Yours.

Determining topology formed in a Graph

0 74


Given a graph containing n edges. The task is to find the topology formed by the given set of edges in that graph.

Examples:

Input: edges = [(1, 2), (2, 3), (3, 1)]
Output: “linear”
Explanation: This test case contains three edges that are connected end-to-end, forming a linear topology.

Input: edges = [(1, 2), (1, 3), (1, 4), (1, 5)]
Output: “star”
Explanation: This test case contains four edges that are all connected to a central node (1), forming a star topology.

Input: edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 1)]
Output: “ring”
Explanation: This test case contains five edges that are connected in a circular fashion, forming a ring topology.

Input: edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1), (1, 3), (2, 4), (3, 5), (4, 6)]
Output: “mesh”
Explanation: This test case contains 10 edges that connect all of the nodes in a complex web, forming a mesh topology.

Approach: To solve the problem follow the below Approach:    

To determine the topology formed by a given set of edges, you will need to analyze the connectivity of the edges and identifies the overall shape or structure that they form. This approach first creates a list of edges and an empty dictionary to store the connections for each node. It then iterates over the edges and updates the connections dictionary, keeping track of which nodes are connected to which other nodes. Finally, it determines the topology based on the number and arrangement of the connections in the dictionary, and prints the result.

Here are the steps that are followed in the pseudocode:

  • Create a list of edges and initialize a dictionary to store the connections for each node.
  • Iterate over the edges and update the connections dictionary. For each edge, add the other node to the list of connections for each of the two nodes that are connected by the edge.
  • Determine the topology based on the number and arrangement of the connections in the dictionary. There are four possible cases:
    • If all of the nodes have exactly two connections, the topology is linear.
    • If all of the nodes have exactly one connection, the topology is star.
    • If all of the nodes have exactly two connections, and the first and last edges are connected end-to-end, the topology is ring.
    • If none of the other cases apply, the topology is mesh.
  • Print the topology.

This code assumes that the input will be a list of edges represented as tuples, where each tuple contains the two nodes that are connected by the edge. The edges are assumed to be undirected, meaning that the order of the nodes in the tuple does not matter. The program will work correctly for any set of edges that meet these assumptions.

Below is the implementation of the above approach:

Python3

def findTopology(edges):

    

    

    connections = {}

  

    

    

    for (node1, node2) in edges:

        if node1 not in connections:

            connections[node1] = [node2]

        else:

            connections[node1].append(node2)

        if node2 not in connections:

            connections[node2] = [node1]

        else:

            connections[node2].append(node1)

  

    

    

    if all(len(v) == 2 for v in connections.values()):

        topology = "linear"

    elif all(len(v) == 1 for v in connections.values()):

        topology = "star"

    elif all(len(v) == 2 for v in connections.values()) and (edges[0][0] == edges[-1][1]):

        topology = "ring"

    else:

        topology = "mesh"

  

    

    return topology

  

  

edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6),

         (6, 1), (1, 3), (2, 4), (3, 5), (4, 6)]

print(findTopology(edges))

  

Complexity Analysis:

  • The Time Complexity for iterating over the edges and updating the connections dictionary has a linear complexity of O(n), where n is the number of edges. For each edge, the program performs a constant amount of work to update the connections dictionary, so the overall complexity is directly proportional to the number of edges. Overall, the complexity of this pseudocode is O(n), which means it will take longer to run as the number of edges increases. This makes it well-suited for small to medium-sized inputs, but it may not be efficient for very large inputs.
  • The auxiliary space of this code is O(n), where n is the number of edges in the input. The main component of the space complexity is the connections dictionary, which is used to store the connections for each node. The size of this dictionary is directly proportional to the number of edges in the input since each edge corresponds to one entry in the dictionary. The other data structures used in the pseudocode, such as the list of edges and the temporary variables used in the loop, have a constant size and do not contribute significantly to the overall space complexity. Overall, the space complexity of this pseudocode is O(n), which means it will use more memory as the number of edges increases. This may be a concern for very large inputs, but it should not be a problem for most practical applications.


Given a graph containing n edges. The task is to find the topology formed by the given set of edges in that graph.

Examples:

Input: edges = [(1, 2), (2, 3), (3, 1)]
Output: “linear”
Explanation: This test case contains three edges that are connected end-to-end, forming a linear topology.

Input: edges = [(1, 2), (1, 3), (1, 4), (1, 5)]
Output: “star”
Explanation: This test case contains four edges that are all connected to a central node (1), forming a star topology.

Input: edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 1)]
Output: “ring”
Explanation: This test case contains five edges that are connected in a circular fashion, forming a ring topology.

Input: edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1), (1, 3), (2, 4), (3, 5), (4, 6)]
Output: “mesh”
Explanation: This test case contains 10 edges that connect all of the nodes in a complex web, forming a mesh topology.

Approach: To solve the problem follow the below Approach:    

To determine the topology formed by a given set of edges, you will need to analyze the connectivity of the edges and identifies the overall shape or structure that they form. This approach first creates a list of edges and an empty dictionary to store the connections for each node. It then iterates over the edges and updates the connections dictionary, keeping track of which nodes are connected to which other nodes. Finally, it determines the topology based on the number and arrangement of the connections in the dictionary, and prints the result.

Here are the steps that are followed in the pseudocode:

  • Create a list of edges and initialize a dictionary to store the connections for each node.
  • Iterate over the edges and update the connections dictionary. For each edge, add the other node to the list of connections for each of the two nodes that are connected by the edge.
  • Determine the topology based on the number and arrangement of the connections in the dictionary. There are four possible cases:
    • If all of the nodes have exactly two connections, the topology is linear.
    • If all of the nodes have exactly one connection, the topology is star.
    • If all of the nodes have exactly two connections, and the first and last edges are connected end-to-end, the topology is ring.
    • If none of the other cases apply, the topology is mesh.
  • Print the topology.

This code assumes that the input will be a list of edges represented as tuples, where each tuple contains the two nodes that are connected by the edge. The edges are assumed to be undirected, meaning that the order of the nodes in the tuple does not matter. The program will work correctly for any set of edges that meet these assumptions.

Below is the implementation of the above approach:

Python3

def findTopology(edges):

    

    

    connections = {}

  

    

    

    for (node1, node2) in edges:

        if node1 not in connections:

            connections[node1] = [node2]

        else:

            connections[node1].append(node2)

        if node2 not in connections:

            connections[node2] = [node1]

        else:

            connections[node2].append(node1)

  

    

    

    if all(len(v) == 2 for v in connections.values()):

        topology = "linear"

    elif all(len(v) == 1 for v in connections.values()):

        topology = "star"

    elif all(len(v) == 2 for v in connections.values()) and (edges[0][0] == edges[-1][1]):

        topology = "ring"

    else:

        topology = "mesh"

  

    

    return topology

  

  

edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6),

         (6, 1), (1, 3), (2, 4), (3, 5), (4, 6)]

print(findTopology(edges))

  

Complexity Analysis:

  • The Time Complexity for iterating over the edges and updating the connections dictionary has a linear complexity of O(n), where n is the number of edges. For each edge, the program performs a constant amount of work to update the connections dictionary, so the overall complexity is directly proportional to the number of edges. Overall, the complexity of this pseudocode is O(n), which means it will take longer to run as the number of edges increases. This makes it well-suited for small to medium-sized inputs, but it may not be efficient for very large inputs.
  • The auxiliary space of this code is O(n), where n is the number of edges in the input. The main component of the space complexity is the connections dictionary, which is used to store the connections for each node. The size of this dictionary is directly proportional to the number of edges in the input since each edge corresponds to one entry in the dictionary. The other data structures used in the pseudocode, such as the list of edges and the temporary variables used in the loop, have a constant size and do not contribute significantly to the overall space complexity. Overall, the space complexity of this pseudocode is O(n), which means it will use more memory as the number of edges increases. This may be a concern for very large inputs, but it should not be a problem for most practical applications.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment