def count_local_triangle(tbl_name, conn): """ input is a matrix """ tol = 0.1 b = 'b' lim = 10 cur = conn.cursor() tn = tbl_name print "create matrix..." create_vector_or_matrix(b, conn) print "Counting dimension..." calc_dim_query = "select max(row), max(col) from %s" % (tn) cur.execute(calc_dim_query) p = cur.fetchone() dim = max(p) + 1 print "dimension is %s" % dim appro = min(dim, lim) for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) print "init b..." lanczos(tn, b, dim, appro, conn) ltriangle = "task7_local_triangle_count" drop_if_exists(ltriangle, conn) cur = conn.cursor() cur.execute("create table %s (nid int, cnt int)" % ltriangle) print "calculating the local triangle count for each node" cur.execute( "insert into %s select U.row, round(sum(power(U.value, 2.0) * power(V.value, 3.0) / 2.0)) from eigenvec U, eigenval V where U.col = V.row and V.row = V.col group by U.row" % (ltriangle))
def test_uci_gamma(self): """http://konect.uni-koblenz.de/networks/ucidata-gama""" data_file = "data/ucidata-gama.txt" gfile = "task5_ucigama" cur = self.conn.cursor() drop_if_exists(gfile, self.conn) cur.execute("create table %s(row int, col int, value real DEFAULT 1)" % (gfile)) cur.execute( "create table tmppppp(row int, col int, value real DEFAULT 1)") cur.copy_from(open(data_file), "tmppppp", columns=('row', 'col', 'value'), sep="\t") cur.execute("insert into %s select row, col from tmppppp" % gfile) drop_if_exists("tmppppp", self.conn) cur.execute("update %s set col=col-1, row=row-1" % gfile) self.conn.commit() print "Uci gama builded ..." reverse_matrix(gfile, self.conn) b = 'b' create_vector_or_matrix(b, self.conn) cur.execute("select max(col) from %s" % gfile) dim = cur.fetchone()[0] + 1 print "Dimension is %s" % dim for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) lanczos(gfile, b, dim, 10, self.conn) drop_if_exists(b, self.conn)
def count_triangle(tbl_name, conn): """ input is a matrix """ tol = 0.1 b = 'b' lim = 10 cur = conn.cursor() tn = tbl_name print "create matrix..." create_vector_or_matrix(b, conn) print "Counting dimension..." calc_dim_query = "select max(row), max(col) from %s" % (tn) cur.execute(calc_dim_query) p = cur.fetchone() dim = max(p) + 1 print "dimension is %s" % dim appro = min(dim, lim); for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) print "init b..." lanczos(tn, b, dim, appro, conn) cur.execute("select power(value,3.0) from eigenval where row = col order by value desc limit %s" % appro) r = cur.fetchall() print r s = 0.0001 for i in range(len(r)): if r[i][0] < 0 or abs(r[i][0]) / s < tol: break s += r[i][0] drop_if_exists(tn, conn) drop_if_exists(b, conn) # drop_if_exists("eigenval", conn) # drop_if_exists("eigenvec", conn) return s / 6.0
def count_local_triangle(tbl_name, conn): """ input is a matrix """ tol = 0.1 b = 'b' lim = 10 cur = conn.cursor() tn = tbl_name print "create matrix..." create_vector_or_matrix(b, conn) print "Counting dimension..." calc_dim_query = "select max(row), max(col) from %s" % (tn) cur.execute(calc_dim_query) p = cur.fetchone() dim = max(p) + 1 print "dimension is %s" % dim appro = min(dim, lim); for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) print "init b..." lanczos(tn, b, dim, appro, conn) ltriangle = "task7_local_triangle_count" drop_if_exists(ltriangle, conn) cur = conn.cursor() cur.execute("create table %s (nid int, cnt int)" % ltriangle) print "calculating the local triangle count for each node" cur.execute("insert into %s select U.row, round(sum(power(U.value, 2.0) * power(V.value, 3.0) / 2.0)) from eigenvec U, eigenval V where U.col = V.row and V.row = V.col group by U.row" % (ltriangle))
def test_lanczos(conn): b = 'b' v = 'v' create_vector_or_matrix(v, conn) clear_table("v", conn) # random_square_matrix(v, dim, conn); cur = conn.cursor() f = open("data/matrix_data.txt") data = [line.strip().split(" ") for line in f] for i in range(len(data)): for j in range(len(data)): cur.execute("insert into %s values (%s, %s, %s)" % (v, i, j, data[i][j])) dim = len(data) create_vector_or_matrix(b, conn) for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) lanczos(v, b, dim, dim, conn)
def test_ucidata(self): data_file = "data/ucidata-zachary.txt" gfile = "task5_ucidata" cur = self.conn.cursor() drop_if_exists(gfile, self.conn) cur.execute("create table %s(row int, col int, value real DEFAULT 1)" % (gfile)) cur.copy_from(open(data_file), gfile, columns=('row', 'col'), sep=" ") cur.execute("update %s set col=col-1, row=row-1" % gfile) self.conn.commit() print "Ucidata builded ..." reverse_matrix(gfile, self.conn) b = 'b' create_vector_or_matrix(b, self.conn) cur.execute("select max(col) from %s" % gfile) dim = cur.fetchone()[0] + 1 print "Dimension is %s" % dim for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) lanczos(gfile, b, dim, 20, self.conn) drop_if_exists(b, self.conn)
def test_youtube(self): """http://snap.stanford.edu/data/com-Youtube.html""" data_file = "data/com-youtube.ungraph.txt" gfile = "task5_youtube" cur = self.conn.cursor() drop_if_exists(gfile, self.conn) cur.execute("create table %s(row int, col int, value real DEFAULT 1)" % (gfile)) cur.copy_from(open(data_file), gfile, columns=('row', 'col'), sep="\t") cur.execute("update %s set col=col-1, row=row-1" % gfile) self.conn.commit() print "Youtube builded ..." reverse_matrix(gfile, self.conn) b = 'b' create_vector_or_matrix(b, self.conn) cur.execute("select max(col) from %s" % gfile) dim = cur.fetchone()[0] + 1 print "Dimension is %s" % dim for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) lanczos(gfile, b, dim, 20, self.conn) drop_if_exists(b, self.conn)
def test_uci_gamma(self): """http://konect.uni-koblenz.de/networks/ucidata-gama""" data_file = "data/ucidata-gama.txt" gfile = "task5_ucigama" cur = self.conn.cursor() drop_if_exists(gfile, self.conn) cur.execute("create table %s(row int, col int, value real DEFAULT 1)" % (gfile)) cur.execute("create table tmppppp(row int, col int, value real DEFAULT 1)") cur.copy_from(open(data_file), "tmppppp", columns=('row', 'col', 'value'), sep="\t") cur.execute("insert into %s select row, col from tmppppp" % gfile) drop_if_exists("tmppppp", self.conn) cur.execute("update %s set col=col-1, row=row-1" % gfile) self.conn.commit() print "Uci gama builded ..." reverse_matrix(gfile, self.conn) b = 'b' create_vector_or_matrix(b, self.conn) cur.execute("select max(col) from %s" % gfile) dim = cur.fetchone()[0] + 1 print "Dimension is %s" % dim for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) lanczos(gfile, b, dim, 10, self.conn) drop_if_exists(b, self.conn)
def count_triangle(tbl_name, conn): """ input is a matrix """ tol = 0.1 b = 'b' lim = 10 cur = conn.cursor() tn = tbl_name print "create matrix..." create_vector_or_matrix(b, conn) print "Counting dimension..." calc_dim_query = "select max(row), max(col) from %s" % (tn) cur.execute(calc_dim_query) p = cur.fetchone() dim = max(p) + 1 print "dimension is %s" % dim appro = min(dim, lim) for i in range(dim): cur.execute("insert into %s values (%s, %s, %s)" % (b, i, 0, 1.0 / float(dim))) print "init b..." lanczos(tn, b, dim, appro, conn) cur.execute( "select power(value,3.0) from eigenval where row = col order by value desc limit %s" % appro) r = cur.fetchall() print r s = 0.0001 for i in range(len(r)): if r[i][0] < 0 or abs(r[i][0]) / s < tol: break s += r[i][0] drop_if_exists(tn, conn) drop_if_exists(b, conn) # drop_if_exists("eigenval", conn) # drop_if_exists("eigenvec", conn) return s / 6.0