-1

I'm currently trying to parallelize an astar algorithm and I'm trying to split the graph based on the number of processes. The structure of the graph is similar to a grid and Here is my attempt

if __name__ == "__main__":
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
file_path = "grid_graph.txt"
graph = None
assigned_nodes = []

if rank == 0:
    graph = load_graph(file_path)


graph = comm.bcast(graph, root=0)
total_nodes = len(graph)
num_processes = comm.Get_size()


base_num_nodes = total_nodes // num_processes
remaining_nodes = total_nodes % num_processes


start_index = rank * base_num_nodes
end_index = (rank + 1) * base_num_nodes

if rank < remaining_nodes:
    start_index += rank
    end_index += rank + 1
else:
    start_index += remaining_nodes
    end_index += remaining_nodes

assigned_nodes = list(graph.keys())[start_index:end_index]

all_assigned_nodes = comm.gather(assigned_nodes, root=0)

if rank == 0:
    for process_rank, nodes in enumerate(all_assigned_nodes):
        print("Process", process_rank, "assigned nodes:", nodes)
print("Process", rank, "assigned nodes:", assigned_nodes)
start = Node((0, 0))
goal = Node((2, 2))

try:
    start_time = time.time()
    path = astar(start, goal, graph)
    print("Process", rank, "Path found:", path)
    print("Process", rank, "Total cost:", total_cost(path, graph))
    print("Process", rank, "Execution time:", time.time() - start_time, "seconds")
except ValueError:
    print("Process", rank, "No path found")

the problem here is that some of the nodes in the subgraphs have no neighbors. How do I partition the graph so that all the nodes in the graph are connected or at least there is a searchable path?

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • Please show, for a small simple graph, the input and output that demonstrates the problem. Also, please clarify "based on the number of processes." What are these "processes"? – ravenspoint Jul 08 '23 at 12:43
  • " nodes in the subgraphs have no neighbors." Perhaps your question should be: "How to split a connected graph so every node has at least one neighbor ( no component is smaller than 2)" – ravenspoint Jul 08 '23 at 12:46
  • Your indentation is clearly wrong, but what’s less clear is how this ought to be indented. Don’t make us guess; please [edit] to fix this. (On the desktop version of this site, paste your program, select it, and type ctrl-K to properly format it as code.) – tripleee Jul 08 '23 at 13:05

2 Answers2

0

IF

your real question is: "How to split a connected graph so every node as at least one neighbor ( no component is smaller than 2)"

THEN

Do not consider for removal any link that connects a node of degree 1.

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
0

If you are interested to the details of the algorithms for graph partitioning, then search for the Louvain algorithm or the Kernighan-Lin algorithm. There are more advanced algorithms, but these two will get you started. You need to be aware that this is a rather complex topic though.

If, instead, you only need to use one graph partitioning algorithm, then search for a graph partitioning library. They are many, including Métis, ParMetis, Zoltan, KaHIP (Karlsruhe High Quality Hypergraph Partitioning, includes many different algorithms), PARTY and Mongoose.

Massimo Cafaro
  • 25,429
  • 15
  • 79
  • 93