Search code examples
prolog

How can I determine centrality in a node graph in Prolog?


First of all this is not homework, just trying to learn Prolog on my own :)

I've been reading about graph theory and I thought it would be cool to implement betweeness centrality in my pet project. I have a list of cities and want to determine which one is more common in all shortest paths possible but I'm not sure how to get all two city combinations.

I already have a rule that gets the shortest path between two cities.


Solution

  • Hmmm ... Betweenness Centrality. Complexity according to Betweenness Centrality -- Incremental and Faster is O(mn + n^2 log n) time, where n = |V| and m = |E| for the common algorithm, but can be improved with incremental edge updates. Interesting.

    But your question is not about Betweenness Centrality but about the Cartesian Product?

    To get the cartesian product of cities, you have to create a goal that chains two member/2 calls, and then collect all solutions thereof with bagof/3:

    cities_1([berlin,tokyo,kigali,moscow]).
    cities_2([beijing,new_delhi,cairo,lisboa]).
    
    cartesian_product(CityPairs) :-
       cities_1(L1), 
       cities_2(L2), 
       bagof([C1,C2],(member(C1,L1),member(C2,L2)),CityPairs).