Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a code that lists kplexes in a given graph, the problem is that it is slow on large graphs like these two:
jazz.txt
karate.txt
Is there any way to improve running time on large graphs, it is fast enough on small graphs like this one
example.txt
Here is the code
Python
import networkx as nx
def readGraph(f):
  G = nx.Graph()
  File = open(f, 'r')
  Lines = File.readlines()
  for line in Lines:
    vertices = line.split()
    if len(vertices) == 1:
      G.add_node(vertices[0])
    else:
      G.add_edge(vertices[0], vertices[1])
  return G
def isKPlex(G, k):
  vertices = G.nodes()
  n = len(vertices)
  for vertex in vertices:
    if len(list(G.neighbors(vertex))) < (n - k):
      return False
  return True
def isMaximalKPlex(G, vertices, k):
    graph = G.subgraph(vertices)
    if not isKPlex(graph, k):
      return False
    nodes = list(G.nodes())
    for node in nodes:
      if not (node in vertices):
        graph = G.subgraph(list(vertices | {node}))
        if isKPlex(graph, k):
          return False
    return True
#Functions that lists kplexes
def listKPlexRecursive(G, k, candidate_set, listOfMaximals, memo=None):
  if memo is None:
    memo = set()
  key = (frozenset(candidate_set), k)  # Using frozenset for memoization
  if key in memo:
    return
  graph = G.subgraph(list(candidate_set))
  if isMaximalKPlex(graph, candidate_set, k) and (candidate_set not in listOfMaximals):
    listOfMaximals.append(candidate_set)
    memo.add(key)
    return
  else:
    if not isKPlex(graph, k) and nx.is_connected(graph):
      for vertex in candidate_set:
        next_set = candidate_set - {vertex}
        listKPlexRecursive(G, k, next_set, listOfMaximals, memo)
  memo.add(key)

def listKPlex(G, k):
  degeneracy_ordering = set(G.nodes())
  listOfMaximals = []
  listKPlexRecursive(G, k, degeneracy_ordering, listOfMaximals)
  return listOfMaximals

def buildMaximal(G, k, P, C, listOfMaximals, excluded):
  if P in listOfMaximals:
    return
  if isMaximalKPlex(G, P, k) and (P not in listOfMaximals):
    listOfMaximals.append(P)
    return
  if C:
    for c in C:
      graph = G.subgraph(list(P | {c}))
      if isKPlex(graph, k):
        buildMaximal(G, k, P | {c}, C - {c}, listOfMaximals, excluded)
      else:
        if P not in listOfMaximals:
          excluded.append(P)
  else:
    return listOfMaximals
def listEnum(G, k):
  if isKPlex(G,k):
    ll = list()
    P = set(G.nodes())
    ll.append(P)
    return ll
  else:
    C = set(G.nodes())
    n = len(C)
    listOfMaximals = list()
    excluded = list()
    for c in C:
      buildMaximal(G, k, {c}, C - {c}, listOfMaximals, excluded)
      neighbors = set(G.neighbors(c))
      buildMaximal(G, k, neighbors | {c}, C - (neighbors | {c}), listOfMaximals, excluded)
      C = C - {c}
    print("Excluded size = " + str(len(excluded)))
    return listOfMaximals
#Main
G = readGraph('/content/example.txt')
vertices = list(G.nodes())
n = len(vertices)
degree_dict = dict(G.degree())
min_degree = min(degree_dict.values())
kmax = n - min_degree
for k in range(kmax, 1, -1):
  print("List for k = " + str(k))
  ll = listKPlex(G, k)
  print(ll)
  print(len(ll))


What I have tried:

I tried it on small graphs and large graphs, but it is only fast on small graphs, any help is appreciated
Posted
Comments
PIEBALDconsult 22-Apr-24 13:01pm    
Write it in a real language.

You need to find a better algorithm. I'm not versed in algorithm complexity analysis, but it would seem you've got something that takes exponential or perhaps, more likely, factorial time based on the number of nodes and edges in the graph. Not really knowing anything about graph theory or k-plexes, I can only suggest you do some research and see if you can find some suggestions on how to efficiently enumerate the k-plexes in a given graph. Clearly, an iterative approach is not workable for large graphs.
 
Share this answer
 
As mentioned already, you need to use a different algorithm. Someone might point you towards something specific but it's unlikely that they will give you a course in graph theory or algorithm optimisation. You're going to have to do some reading.

From everyone's favourite AI...

There are several efficient algorithms for calculating k-plexes in large graphs, whilst they are generally complex due to the nature of the problem, there are some simpler algorithms that an inexperienced person might be able to implement with some effort. Here are a few suggestions:

1. Edmonds-Karp Algorithm: This is a variant of the Ford-Fulkerson method for finding the maximum flow in network graphs³. It's not specifically designed for k-plexes, but it's a fundamental graph algorithm that could be a good starting point for understanding more complex algorithms.

2. Heuristic Methods: Some papers mention the use of heuristic methods for solving the maximum k-plex problem². These methods might be easier to implement than exact algorithms, but they may not always find the optimal solution.

3. ListPlex Algorithm: This algorithm lists all maximal k-plexes¹. While it's more complex than the previous two, the paper provides a detailed explanation that might be helpful for an inexperienced person willing to take on a challenge¹.

Remember, implementing these algorithms will require a solid understanding of graph theory and programming. If you're new to these topics, I recommend starting with simpler problems and gradually working your way up to more complex ones like k-plexes. Happy coding! 😊

(1) Network Flow: Edmonds-Karp Algorithm - Baeldung. https://www.baeldung.com/cs/network-flow-edmonds-karp-algorithm[^] .
(2) An Exact Algorithm for Maximum k-Plexes in Massive Graphs - IJCAI. https://www.ijcai.org/Proceedings/2018/0201.pdf[^] .
(3) Listing Maximal k-Plexes in Large Real-World Graphs - arXiv.org. https://arxiv.org/pdf/2202.08737[^] .
(4) undefined. Listing Maximal k-Plexes in Large Real-World Graphs | Proceedings of the ACM Web Conference 2022[^] .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900