Ejemplo n.º 1
0
def create_rnd_trees(size, number, filename, dst_path, labeled=False, seed=1):
    random.seed(seed)
    for i in range(number):
        G = nx.random_tree(size, seed + i)
        GSnap = snap.TUNGraph()

        if labeled:
            labels = snap.TIntStrH()
        for node in G.nodes(data=True):
            id = node[0]
            if labeled:
                node_label = node[1]['predicate']
                labels[int(id)] = str(node_label)
            GSnap.AddNode(int(id))

        for edge in G.edges():
            GSnap.AddEdge(int(edge[0]), int(edge[1]))

        FOut = snap.TFOut(dst_path + filename + "_" +
                          str(i).zfill(math.ceil(math.log10(number + 1))) +
                          ".graph")
        GSnap.Save(FOut)
        FOut.Flush()

        if labeled:
            FOut = snap.TFOut(dst_path + filename + "_" +
                              str(i).zfill(math.ceil(math.log10(number + 1))) +
                              ".labels")
            labels.Save(FOut)
            FOut.Flush()
Ejemplo n.º 2
0
def gml_gpickle_to_snap_graph(filename, src_path, dst_path=""):
    if dst_path == "":
        dst_path = src_path

    try:
        G = nx.read_gpickle(src_path + filename + ".gpickle")
    except:
        G = nx.read_gml(src_path + filename + ".gml")

    GSnap = snap.TUNGraph()

    labels = snap.TIntStrH()
    for node in G.nodes(data=True):
        id = node[0]
        node_label = node[1]['predicate']
        labels[int(id)] = str(node_label)
        GSnap.AddNode(int(id))

    for edge in G.edges():
        GSnap.AddEdge(int(edge[0]), int(edge[1]))

    FOut = snap.TFOut(dst_path + filename + ".graph")
    GSnap.Save(FOut)

    FOut = snap.TFOut(dst_path + filename + ".labels")
    labels.Save(FOut)
    FOut.Flush()
Ejemplo n.º 3
0
def DefaultConstructor():
    '''
    Test the default constructor
    '''

    Graph = snap.TUNGraph()
    PrintGStats("DefaultConstructor:Graph", Graph)
Ejemplo n.º 4
0
def GetSmallGraph():
    '''
    Test small graph
    '''

    Graph = snap.TUNGraph()
    Graph.GetSmallGraph()
    PrintGStats("GetSmallGraph:Graph", Graph)
def constructGraph(dic):
	G = snap.TUNGraph().New()
	for v, val in dic.iteritems():
		if not G.IsNode(v): G.AddNode(v)
		for w in val.keys():
			if not G.IsNode(w): G.AddNode(w)
			G.AddEdge(v, w)
	print snap.GetClustCf(G, -1)
	return G
 def buildSnapGraph(self, networkxGraph=None):
     '''
     Creates a SNAP graph object from a NetworkX Graph Object
     '''
     if networkxGraph == None:
         networkxGraph, H = self.buildNxGraph()
     # Initialize empty "SNAP" Graph
     G = snap.TUNGraph().New()
     # Add nodes to empty Graph
     for n in list(networkxGraph.nodes):
         G.AddNode(n)
     # Add edges to graph
     for src, trgt in list(networkxGraph.edges):
         G.AddEdge(src, trgt)
     print("SNAP Graph construction complete.")
     return G, H
Ejemplo n.º 7
0
def load_map(railroad_map_path):
    weight = {}  # weight of edges in the railroad graph
    with open(railroad_map_path, "r") as f:
        header = True
        for line in f:
            if header:
                header = False
                N, M = map(int, line.split())
                G = snap.TUNGraph().New()
                for i in range(1, N + 1):
                    G.AddNode(i)
            else:
                u, v, w = map(int, line.split())
                G.AddEdge(u, v)
                weight[(u, v)] = w
                weight[(v, u)] = w
    return G, N, M, weight
def genCircle(N=5242):
    """
    :param - N: number of nodes

    return type: snap.PUNGraph
    return: Circle graph with N nodes and N edges. Imagine the nodes form a
        circle and each node is connected to its two direct neighbors.
    """
    ############################################################################
    # TODO: Your code here!
    Graph = snap.TUNGraph().New()
    for i in range(N):
        Graph.AddNode(i)

    for u in range(N):
        Graph.AddEdge(u, (u + 1) % N)

    ############################################################################
    return Graph
Ejemplo n.º 9
0
def create_rnd_trees(size, number, dst_path, seed=1):
    """
    Creates some number of random trees of some size
    :param size: size of the random trees
    :param number: number of random trees to generate
    :param dst_path: output path for the patterns
    :param seed: seed for the randomness
    """
    random.seed(seed)
    for i in range(number):
        G = nx.random_tree(size, seed + i)
        GSnap = snap.TUNGraph()
        for edge in G.edges():
            GSnap.AddEdge(int(edge[0]), int(edge[1]))

        FOut = snap.TFOut(dst_path + "rnd_tree" + "_" +
                          str(i).zfill(math.ceil(math.log10(number + 1))) +
                          ".graph")
        GSnap.Save(FOut)
        FOut.Flush()
def genErdosRenyi(N=5242, E=14484):
    """
    :param - N: number of nodes
    :param - E: number of edges

    return type: snap.PUNGraph
    return: Erdos-Renyi graph with N nodes and E edges
    """
    ############################################################################
    # TODO: Your code here!
    Graph = snap.TUNGraph().New()
    for i in range(N):
        Graph.AddNode(i)

    adj = createAdjacencyMatrix(N, E)
    for k1, v1 in adj.items():
        for k2, v2 in adj[k1].items():
            Graph.AddEdge(int(k1), int(k2))

    ############################################################################
    return Graph
Ejemplo n.º 11
0
def genErdosRenyi(N=5242, E=14484):
    """
    :param - N: number of nodes
    :param - E: number of edges

    return type: snap.PUNGraph
    return: Erdos-Renyi graph with N nodes and E edges
    """
    ############################################################################
    # TODO: Your code here!
    Graph = snap.TUNGraph().New()
    nodes = list(range(N))
    for n in nodes:
        Graph.AddNode(n)
    for _ in range(E):
        while True:
            e = np.random.choice(nodes, size=(2, ), replace=False)
            if not Graph.IsEdge(int(e[0]), int(e[1])):
                break
        # print(type(int(e[0])),int(e[0]))
        Graph.AddEdge(int(e[0]), int(e[1]))
    ############################################################################
    return Graph
Ejemplo n.º 12
0
def ManipulateNodesEdges():
    '''
    Test node, edge creation
    '''

    NNodes = 10000
    NEdges = 100000
    FName = "test.graph"

    Graph = snap.TUNGraph()
    t = Graph.Empty()

    # create the nodes
    for i in range(0, NNodes):
        Graph.AddNode(i)

    t = Graph.Empty()
    n = Graph.GetNodes()

    # create random edges
    NCount = NEdges
    while NCount > 0:
        x = int(random.random() * NNodes)
        y = int(random.random() * NNodes)
        # skip the loops in this test
        if x != y and not Graph.IsEdge(x, y):
            n = Graph.AddEdge(x, y)
            NCount -= 1

    PrintGStats("ManipulateNodesEdges:Graph1", Graph)

    # get all the nodes
    NCount = 0
    NI = Graph.BegNI()
    while NI < Graph.EndNI():
        NCount += 1
        NI.Next()

    # get all the edges for all the nodes
    ECount1 = 0
    NI = Graph.BegNI()
    while NI < Graph.EndNI():
        ECount1 += NI.GetOutDeg()
        NI.Next()

    ECount1 = ECount1 / 2

    # get all the edges directly
    ECount2 = 0
    EI = Graph.BegEI()
    while EI < Graph.EndEI():
        ECount2 += 1
        EI.Next()

    print "graph ManipulateNodesEdges:Graph2, nodes %d, edges1 %d, edges2 %d" % (
        NCount, ECount1, ECount2)

    # assignment
    Graph1 = Graph
    PrintGStats("ManipulateNodesEdges:Graph3", Graph1)

    # save the graph
    FOut = snap.TFOut(snap.TStr(FName))
    Graph.Save(FOut)
    FOut.Flush()

    # load the graph
    FIn = snap.TFIn(snap.TStr(FName))
    Graph2 = snap.TUNGraph(FIn)
    PrintGStats("ManipulateNodesEdges:Graph4", Graph2)

    # remove all the nodes and edges
    for i in range(0, NNodes):
        n = Graph.GetRndNId()
        Graph.DelNode(n)

    PrintGStats("ManipulateNodesEdges:Graph5", Graph)

    Graph1.Clr()
    PrintGStats("ManipulateNodesEdges:Graph6", Graph1)
Ejemplo n.º 13
0
def Empty_graph(n):
    G = snap.TUNGraph().New()
    for i in range(n):
        G.AddNode(i)

    return G