0

In this problem, I have data as:

number_list = {
    'Room 1' : ['Washroom 1','Balcony 1'],
    'Room 2' : ['Gallary 1'],
    'Room 3' : ['Washroom 2'],
    'Kitchen' : ['Store 1', 'Garden 1'],
    'Garden' : ['Entrance'],
    'Hall' : ['Store 2']
}

I got from for Room 1 where Washroom 1 and balcony is connected.

I want to make a code, where I want, these keys, which is: Room1, Room2, Room3, Kitchen, Garden, hall connected to each other randomly or using probability

How to do this?

Thanks

  • Any suggestions? Please guide me how will I make graph for this using networkx – XCugas Team Jun 03 '21 at 04:49
  • Welcome to Stack Overflow! :^) Your question is a bit more broad than those typically answered here, but I'll try to provide some guidance. For this broad of a problem, it's helpful to post how you've attempted to solve this and what specific problems you're encountering. See also, [How to ask a good question](https://stackoverflow.com/help/how-to-ask). – Frodnar Jun 03 '21 at 14:47
  • A graph can simply be initialized from a dictionary of the type you've posted as in [this SO answer](https://stackoverflow.com/a/26665960/15534441). So your question then becomes, "How do I randomly or with set probabilities generate this type of dictionary?" Without more requirements, it's hard to say how to do this. You could think about making a `set` or `list` of unique room names and randomly generating pairs of rooms to connect then using those to build the `dict` key-value pairs. – Frodnar Jun 03 '21 at 14:55
  • @Frodnar Thanks for the response. I have checked the link. As, I mentioned, I wants to connect the keys only in sequential manner. For eg: (Room 1, Room 2), (Room 2, Room 3), (Room 3, Kitchen), (Kitchen, Garden), (Garden, Hall) Just like this. I have tried to use max_weight_matching but this didnt work for me. How to connect unconnected node in sequence form – XCugas Team Jun 03 '21 at 15:50

1 Answers1

0

If I understand your problem correctly, the following code should achieve the desired goal if you are using Python 3.6 or higher (since dict objects preserve order).

import networkx as nx

number_list = {
    'Room 1' : ['Washroom 1','Balcony 1'],
    'Room 2' : ['Gallary 1'],
    'Room 3' : ['Washroom 2'],
    'Kitchen' : ['Store 1', 'Garden 1'],
    'Garden' : ['Entrance'],
    'Hall' : ['Store 2']
}

G = nx.Graph(number_list)

# Build sequential pairs of rooms to connect.  Python 3.6+ only!
rooms_to_connect = zip(list(number_list.keys())[:-1], list(number_list.keys())[1:])

G.add_edges_from(rooms_to_connect)
nx.draw(G, with_labels=True)

which plots G to confirm that we're seeing what we expected: connected_rooms_graph

Frodnar
  • 2,129
  • 2
  • 6
  • 20