Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. #import networkx as nx
  2. #from networkx.algorithms import isomorphism
  3. from graph_tool.all import *
  4. import graph_tool.all as gt
  5. #from collections import Counter
  6. import time
  7. ti=time.time()
  8. # read graphs from file.
  9. def readGraphFile(graphFile):
  10.     G_list = []
  11.     indice = []
  12.     v_indice = []
  13.     e_indice=[]
  14.     frequence_list = []
  15.     frequentgraphs = []
  16.     frequentfreqs=[]
  17.     #appearance = [] #store the appearance of the frequent pattern
  18.     fp = open(graphFile, "r+")
  19.     lines = [line for line in fp.read().splitlines() if line]
  20.     for line in lines:
  21.         data = line.split()
  22.         if data[0] == 't':
  23.             vdic={}
  24.             vdic.clear()
  25.             if (len(data) < 3):
  26.                 print("Graph header line error...")
  27.             else:
  28.                 g = Graph()
  29.                 v_label = g.new_vertex_property("int")              
  30.                 v_num = g.new_vertex_property("int")
  31.                 e_label = g.new_edge_property("int")
  32.                 G_list.append(g)
  33.                 indice.append(int(data[2]))
  34.                 #G_list[ map(int, data[4:])] = g
  35.                
  36.         elif data[0] == 'v':
  37.             data = line.split()            
  38.             if (len(data) < 3):
  39.                 print 'Node data line error...'
  40.             else:
  41.                 #g.add_node(data[1], attrib = data[2])
  42.                 v=g.add_vertex()
  43.                 v_num[v]= int(data[1])              
  44.                 v_label[v] = int(data[2])
  45.                 vdic[int(data[1])]=int(data[2])
  46.                 #as node graph transaction format is single value, use attrib as a common noun for all attrib
  47.         elif data[0] == 'e':           
  48.             if (len(data) < 4):
  49.                 print 'Edge data line error...'
  50.             else:
  51.                 #print(vdic)
  52.                 v1=g.add_vertex()              
  53.                 #vdic[int(data[1])]
  54.                 v_label[v1] = vdic[int(data[1])]                
  55.                 v2=g.add_vertex()
  56.                 v_label[v2] = vdic[int(data[2])]
  57.                 e1 = g.add_edge(v1,v2)
  58.                 e_label[e1]=int(data[3])              
  59.    
  60.         #else:
  61.             #print line
  62.             #print '!!! Invalid graph data line...!!!'
  63.     graphdic = dict(zip(G_list, indice))
  64.  
  65.     #print(graphdic)  
  66.        
  67.     print '= = = = = Finished reading {} graphs from the file'.format(len(G_list)) + '= = = = ='
  68.  
  69. ####Frequency Algorithm #####
  70.     ta=time.time()    
  71.     i=0
  72.     cnt=0
  73.     #inc=0;    
  74.  
  75.     for gr in G_list:                
  76.         for c in G_list:
  77.             if (gt.isomorphism(gr,c)):
  78.                 cnt+= 1                            
  79.                 print("iso")
  80.         print ("graph:{} frequence:{} ".format(i,cnt))        
  81.         frequence_list.append(cnt)
  82.         cnt=0
  83.         i+= 1
  84.         print("-----------------------")
  85.          
  86.    
  87.     freqdic = dict(zip(G_list,frequence_list))
  88.     print("\n=x=x=x=x=x=x=x=x=x=x=x=x=")
  89.     #try: seuil=float(raw_input('Entrer un Sieul:'))
  90.     #except ValueError: print ("float")
  91.     for gr, fr in freqdic.iteritems():
  92.         if fr >= 2:
  93.             frequentgraphs.append(gr)
  94.             frequentfreqs.append(fr)
  95.  
  96.     frequentgraphsdic= dict(zip(frequentgraphs,frequentfreqs))
  97.     print("\n=x=x=x=x=x=x=x=x=x=x=x=x=\n frequent items:{}\n".format(len(frequentgraphs)))
  98.     #print(frequentgraphsdic)
  99.    
  100.     print("* * * * * D O N E * * * * * ")    
  101.     print("RUNNING TIME: {}s".format(time.time()-ti))    
  102.     print("Reading from file: {}s".format(ta-ti))    
  103.     print("Algo: {}s".format(time.time()-ta))    
  104.    
  105. #############################    
  106.  
  107.    
  108.  
  109.  
  110.  
  111. #programme pricipale    
  112. def  main():
  113.     readGraphFile("5.txt")
  114.        
  115. if __name__ == '__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement