0

Suppose we need to draw a graph with many points. For example INPUT : {1#2,2#3,3#11,1#11,4#11,4#5,5#6,4#12} OUTPUT : 7

One node can be connected to many other nodes directly. We need to find the max connected nodes in this graph but not allowed to go back.

I have tried a lot to get any algorithm to solve this problem but not able to find. Can someone please help me for this?

Thanks in Advance, Krishan

  • What do you mean by 'max' number? You need to clarify what exactly the input and output is. Are you given an edge list? A node count? – VoidStar Apr 09 '15 at 05:11
  • We need to find out the max possible length which traversed in the graph designed by given input point. For example INPUT {1#2,2#3,3#11,1#11,4#11,4#5,5#6,4#12} should output 7 as result – Krishan Kumar Gorav Apr 09 '15 at 07:35

2 Answers2

0

Your definition of the problem is still a bit vague - at least from my point of view. However, I think that you are looking for the longest path in a (directed or undirected?) graph. In general, this is a NP-complete problem. Have a look at this wikepedia entry. This should serve as a starting point for further research.

Reinhard
  • 409
  • 2
  • 10
0

Here is the python code that will find the number of maximum components (maximum connected nodes) in a graph. However, it is not optimal solution(a.k.a, slow), but might be useful for you to get started

#!/bin/python
max_count = 0

def count_components(adj_list,r,c,count):
    global max_count
    row  = adj_list[r]

    #count
    connections = 0
    for j in range(len(row)):
        if row[j]==1:
            connections+=1
    count+=connections

    #traverse
    for j in range(len(row)):
        if row[j]==1:            
            adj_list[j][r]=0
            count_components(adj_list,j,r,count)
    #print 'count = {}'.format(count)
    if count>max_count:
        max_count = count

def get_max_num_of_connected_component(adj_list):

    for i in range(len(adj_list)):
        row = adj_list[i]
        for j in range(len(row)):
            if row[j]==1:
                count_components(adj_list,i,j,1)
                break

        #print_adjecency_list(adj_list)
    #print 'max_count = {}'.format(max_count)
    return max_count
def print_adjecency_list(adj_list):
    for i in range(len(adj_list)):
        row = adj_list[i]
        for j in range(len(row)):
            print row[j],
        print
    print
import sys

n, m = raw_input().strip().split(' ')
n, m = [int(n), int(m)]
route = []
for route_i in xrange(m):
    route_temp = map(int,raw_input().strip().split(' '))
    route.append(route_temp)
# Write Your Code Here

# generate NxM adjecency list of 
adjecency_list=[]
for i in range(n):
    row = []
    for j in range(n):
        row.append(0)
    adjecency_list.append(row)

for road in route:
    r = road[0]-1
    c = road[1]-1
    adjecency_row = adjecency_list[r]
    adjecency_row[c]=1
    adjecency_list[r] = adjecency_row

    r = road[1]-1
    c = road[0]-1
    adjecency_row = adjecency_list[r]
    adjecency_row[c]=1
    adjecency_list[r] = adjecency_row

#print_adjecency_list(adjecency_list)

print get_max_num_of_connected_component(adjecency_list)
Jumabek Alikhanov
  • 2,305
  • 3
  • 21
  • 21