Given a graph containing n edges. The duty is to search out the topology fashioned by the given set of edges in that graph.
Examples:
Enter: edges = [(1, 2), (2, 3), (3, 1)]
Output: “linear”
Rationalization: This take a look at case incorporates three edges which are linked end-to-end, forming a linear topology.Enter: edges = [(1, 2), (1, 3), (1, 4), (1, 5)]
Output: “star”
Rationalization: This take a look at case incorporates 4 edges which are all linked to a central node (1), forming a star topology.Enter: edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 1)]
Output: “ring”
Rationalization: This take a look at case incorporates 5 edges which are linked in a round trend, forming a hoop topology.Enter: edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1), (1, 3), (2, 4), (3, 5), (4, 6)]
Output: “mesh”
Rationalization: This take a look at case incorporates 10 edges that join all the nodes in a posh net, forming a mesh topology.
Strategy: To unravel the issue comply with the under Strategy:
To find out the topology fashioned by a given set of edges, you will have to investigate the connectivity of the sides and identifies the general form or construction that they type. This method first creates a listing of edges and an empty dictionary to retailer the connections for every node. It then iterates over the sides and updates the connections dictionary, maintaining observe of which nodes are linked to which different nodes. Lastly, it determines the topology primarily based on the quantity and association of the connections within the dictionary, and prints the consequence.
Listed here are the steps which are adopted within the pseudocode:
- Create a listing of edges and initialize a dictionary to retailer the connections for every node.
- Iterate over the sides and replace the connections dictionary. For every edge, add the opposite node to the record of connections for every of the 2 nodes which are linked by the sting.
- Decide the topology primarily based on the quantity and association of the connections within the dictionary. There are 4 attainable instances:
- If all the nodes have precisely two connections, the topology is linear.
- If all the nodes have precisely one connection, the topology is star.
- If all the nodes have precisely two connections, and the primary and final edges are linked end-to-end, the topology is ring.
- If not one of the different instances apply, the topology is mesh.
- Print the topology.
This code assumes that the enter will probably be a listing of edges represented as tuples, the place every tuple incorporates the 2 nodes which are linked by the sting. The perimeters are assumed to be undirected, that means that the order of the nodes within the tuple doesn’t matter. This system will work appropriately for any set of edges that meet these assumptions.
Under is the implementation of the above method:
Python3
def findTopology(edges):
# Initialize a dictionary to retailer the
# connections for every node
connections = {}
# Iterate over the sides and replace
# the connections dictionary
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)
# Decide the topology primarily based
# on the connections
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"
# Print the topology
return topology
# Create a listing of edges
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
(6, 1), (1, 3), (2, 4), (3, 5), (4, 6)]
print(findTopology(edges))
# this code is contributed by prophet1999
Complexity Evaluation:
- The Time Complexity for iterating over the sides and updating the connections dictionary has a linear complexity of O(n), the place n is the variety of edges. For every edge, this system performs a relentless quantity of labor to replace the connections dictionary, so the general complexity is straight proportional to the variety of edges. General, the complexity of this pseudocode is O(n), which suggests it should take longer to run because the variety of edges will increase. This makes it well-suited for small to medium-sized inputs, but it surely might not be environment friendly for very giant inputs.
- The auxiliary house of this code is O(n), the place n is the variety of edges within the enter. The principle part of the house complexity is the connections dictionary, which is used to retailer the connections for every node. The scale of this dictionary is straight proportional to the variety of edges within the enter since every edge corresponds to 1 entry within the dictionary. The opposite knowledge buildings used within the pseudocode, such because the record of edges and the momentary variables used within the loop, have a relentless measurement and don’t contribute considerably to the general house complexity. General, the house complexity of this pseudocode is O(n), which suggests it should use extra reminiscence because the variety of edges will increase. This can be a priority for very giant inputs, but it surely shouldn’t be an issue for many sensible purposes.