Example #1
0
    def __init__(self, user_url):
        self.user_url = user_url

        self.user_network = DirectedGraph()
        self.user = User(user_url)
        self.user.make_followers()
        self.user.make_following()
Example #2
0
    def import_graph(self, file_name, process=lambda progress: None):
        try:
            file_graph = open(file_name + ".txt", "r")
            lines = file_graph.readlines()
            first_line = lines.pop(0).strip()

            graph_params = first_line.split()
            imported_graph = DirectedGraph(int(graph_params[0]))
            total_lines = len(lines)
            last_percent = 0

            for index, line in enumerate(lines):
                new_percent = index * 100 // total_lines
                if last_percent != new_percent:
                    process(new_percent)
                    last_percent = new_percent

                data = line.strip().split(" ")
                imported_graph.add_edge(int(data[0]), int(data[1]),
                                        int(data[2]))

            self.graphs.append(imported_graph)

            file_graph.close()
        except FileNotFoundError:
            raise Exception("File '" + file_name + "' not found")
 def setUp(self):
     self.graph = DirectedGraph()
     self.nodeA = "A"
     self.nodeB = "B"
     self.nodeC = "C"
     self.nodeD = "D"
     self.nodeE = "E"
Example #4
0
 def inOutDegree(graph: DirectedGraph):
     '''
     Function that calculate in and out dege
     '''
     vertex = int(input("    vertex : "))
     print("OUT degree:", graph.outDegreeOf(vertex))
     print("IN degree:", graph.inDegreeOf(vertex), "\n")
Example #5
0
def read_graph(filename='graph.txt'):
    file = open(filename, 'r')
    # read the first line containing the number of vertices and number of edges
    line = file.readline()
    # remove the newline from the end of the string and splitting its parameters
    line = line.strip('\n')
    line = line.split(' ')
    if len(line) != 2:
        raise ValueError('invalid number of vertices and edges')
    vertices = int(line[0])
    edges = int(line[1])
    # create a directed graph with given vertices
    graph = DirectedGraph(vertices)
    for i in range(edges):
        edge = file.readline()
        edge = edge.strip('\n')
        edge = edge.split(' ')
        if len(edge) != 3:
            raise ValueError('invalid line in file!')
        start_vertex = int(edge[0])
        end_vertex = int(edge[1])
        cost = int(edge[2])
        graph.add_edge(start_vertex, end_vertex, cost)
    file.close()
    return deepcopy(graph)
Example #6
0
 def test_in_degree_directed(self):
     graph = DirectedGraph(7)
     graph.add_edge(0, 1)
     graph.add_edge(0, 5)
     self.assertEqual(graph.in_degree(0), 0)
     self.assertEqual(graph.in_degree(1), 1)
     self.assertEqual(graph.in_degree(1), 1)
Example #7
0
 def modifyCost(graph: DirectedGraph):
     '''
     Function that modify the cost of an edge 
     '''
     edgeID = int(input("    edge id : "))
     newCost = int(input("    new cost : "))
     graph.modifyCostOf(edgeID, newCost)
     print("The new cost of {0} is {1}\n".format(edgeID, graph.getCostOf(edgeID)))
Example #8
0
 def removeEdge(graph: DirectedGraph):
     '''
     Function that remove an edge 
     '''
     vS = int(input("    starting edge : "))
     vE = int(input("    end edge : "))
     graph.removeEdge(vS, vE)
     print("Edge {0} -> {1} deleted!".format(vS, vE))
 def setUp(self):
     self.fr = DirectedGraph()
     self.fr.add_edge('sten', 'sho')
     self.fr.add_edge('sten', 'agito')
     self.fr.add_edge('sho', 'zina')
     self.fr.add_edge('sho', 'tom')
     self.fr.add_edge('tom', 'jerry')
     self.fr.add_edge('zina', 'agito')
     self.fr.add_edge('zina', 'sho')
Example #10
0
def EM_run_0():
	print "BFS test"
	graph = DirectedGraph(5, [0.5 ,0.5])
	graph.draw_edge(0, 2)
	graph.draw_edge(1, 2)
	graph.draw_edge(2, 3)
	graph.draw_edge(2, 4)
	
	graph.all_nodes[1].count_all_predecessor()
class TestGraph(unittest.TestCase):

    def setUp(self):
        self.graph = DirectedGraph()
        self.graph.add_edge('Ivan', 'Ivo')
        self.graph.add_edge('Ivan', 'Joro')
        self.graph.add_edge('Joro', 'Krasi')
        self.graph.add_edge('Krasi', 'Rado')

    def test_add_edge(self):
        self.assertIn('Ivan', self.graph.nodes)
        self.assertIn('Ivo', self.graph.nodes)
        self.assertIn('Ivo', self.graph.nodes['Ivan'])

    def test_get_neighbours_for(self):
        self.assertSetEqual(
            self.graph.get_neighbours_for('Ivan'), {'Ivo', 'Joro'})

    def test_path_between_exists(self):
        self.assertTrue(self.graph.path_between('Ivan', 'Rado'))

    def test_path_between_not_exist(self):
        self.assertFalse(self.graph.path_between('Krasi', 'Joro'))

    def test_str_graph(self):
        print(self.graph)
class TestGraph(unittest.TestCase):
    def setUp(self):
        self.graph = DirectedGraph()
        self.graph.add_edge('Ivan', 'Ivo')
        self.graph.add_edge('Ivan', 'Joro')
        self.graph.add_edge('Joro', 'Krasi')
        self.graph.add_edge('Krasi', 'Rado')

    def test_add_edge(self):
        self.assertIn('Ivan', self.graph.nodes)
        self.assertIn('Ivo', self.graph.nodes)
        self.assertIn('Ivo', self.graph.nodes['Ivan'])

    def test_get_neighbours_for(self):
        self.assertSetEqual(self.graph.get_neighbours_for('Ivan'),
                            {'Ivo', 'Joro'})

    def test_path_between_exists(self):
        self.assertTrue(self.graph.path_between('Ivan', 'Rado'))

    def test_path_between_not_exist(self):
        self.assertFalse(self.graph.path_between('Krasi', 'Joro'))

    def test_str_graph(self):
        print(self.graph)
Example #13
0
 def test_dfs_directed(self):
     graph = DirectedGraph(7)
     graph.add_edge(0, 6)
     graph.add_edge(0, 2)
     graph.add_edge(0, 1)
     graph.add_edge(0, 5)
     graph.add_edge(6, 4)
     graph.add_edge(4, 5)
     graph.add_edge(4, 3)
     graph.add_edge(5, 3)
     self.assertEqual(graph.dfs(0), [None, 0, 0, 5, 6, 4, 0])
Example #14
0
 def test_bfs_directed(self):
     graph = DirectedGraph(6)
     graph.add_edge(0, 2)
     graph.add_edge(0, 1)
     graph.add_edge(0, 5)
     graph.add_edge(2, 1)
     graph.add_edge(2, 3)
     graph.add_edge(2, 4)
     graph.add_edge(4, 3)
     graph.add_edge(5, 3)
     self.assertEqual(graph.bfs(0), [None, 0, 0, 2, 2, 0])
Example #15
0
 def addVertex(graph: DirectedGraph):
     '''
     Function that add a vertex 
     '''
     while True:
         vertex = int(input("   vertex id : "))
         if graph.vertexExists(vertex):
             print("This vertex already exists!")
         else:
             graph.addVertex(vertex)
             print("The new vertex is named ", graph.vertices)
             break
Example #16
0
 def removeVertex(graph: DirectedGraph):
     '''
     Function that remove a vertex
     '''
     while True:
         vertex = int(input("   vertex id : "))
         if graph.vertexExists(vertex):
             graph.removeVertex(vertex)
             print("Deletion completed")
             break
         else:
             print("This vertex does not exists!")
Example #17
0
 def edgeBetween(graph: DirectedGraph):
     '''
     Functions that check if there is an edge between 2 vertex
     '''
     x = int(input("    vertexStart : "))
     y = int(input("    vertexIn    : "))
     if graph.vertexExists(x) and graph.vertexExists(y):
         if graph.isEdge(x, y):
             print("There is an edge!\n")
         else:
             print("There is not an edge!\n")
     else:
         print("One or both vertices does not exists!")
Example #18
0
 def addEdge(graph: DirectedGraph):
     '''
     Function that add an edge  
     '''
     while True:
         vS = int(input("    starting edge : "))
         vE = int(input("    end edge : "))
         cost = int(input("      cost : "))
         id = int(input("    id : "))
         try:
             graph.addEdge(vS, vE, cost, id)
             break
         except Exception as ex:
             print(ex)
 def get_order(self, dict):
     """
     :param dict: List[String]
     :return: List[String]
     """
     g = DirectedGraph(26)
     for i in range(0, len(dict)-1):
         self.build_graph(g, dict[i], dict[i+1])
     # order must exist, no need to check cycle
     order = g.topo_sort()
     ret = []
     for c in order:
         ret.append(chr(c + ord('a')))
     return ret
class GetFollowers():

    def __init__(self):
        self.dict_of_following = {}
        self.list_of_follwoers_names = []
        self.dg = DirectedGraph()

    def folling(self, user_name):
        git_user_url = "https://api.github.com/users/{}/following".format(user_name)
        my_request = requests.get(git_user_url, auth=(TOKEN, ''))
        #print(my_request.encoding)
        if my_request.status_code == 200:
            self.dict_of_following = my_request.json()
            #print(self.dict_of_following)
            self.list_of_follwoers_names = []
            for user in self.dict_of_following:
                self.list_of_follwoers_names.append(user["login"])
            return self.list_of_follwoers_names
        else:
            print("f**k you")

    def metoda(self, user):

        for follower in self.folling(user):
            self.dg.add_edge(user, follower)
            for follower2 in self.folling(follower):
                self.dg.add_edge(follower, follower2)
                for follower3 in self.folling(follower2):
                    self.dg.add_edge(follower2, follower3)
                    for follower4 in self.folling(follower3):
                        self.dg.add_edge(follower3, follower4)

    def print_following(self):
        for person in self.dict_of_following:
            print(person["login"])
class DirectedGraphTestClass(unittest.TestCase):

    def setUp(self):
        self.test_graph_info = {"a": ["b", "c"], "b": ["c"], "c": ["d"], "d": []}
        self.test_graph = DirectedGraph()
        self.test_graph.graph = self.test_graph_info

    def test_init(self):
        self.assertTrue(isinstance(self.test_graph, DirectedGraph))

    def test_add_node(self):
        self.test_graph.add_node("p")
        self.assertTrue("p" in self.test_graph.graph.keys())

    def test_add_edge(self):
        self.test_graph.add_edge("c", "a")
        self.assertTrue("a" in self.test_graph.graph["c"])

    def test_get_neighbours_for(self):
        self.assertEqual(self.test_graph.get_neighbours_for("a"), ["b", "c"])

    def test_path_between(self):
        self.assertTrue(self.test_graph.path_between("b", "c"))
        self.assertFalse(self.test_graph.path_between("c", "b"))
        self.assertTrue(self.test_graph.path_between("a", "d"))
Example #22
0
class GithubFollowGraph:

    FOLLOWING_URL = 'https://api.github.com/users/{}/following'
    with open('token.txt', 'r') as token_file:
        content = token_file.readline()[:-1]
        token = (content, '')

    def __init__(self, username, depth):
        self.graph = DirectedGraph()
        self.main_user = username
        self.depth = depth
        followers = [self.main_user]
        current_depth = 0
        while current_depth < depth:
            new_followers = []
            for follower in followers:
                followings = GithubFollowGraph._get_following(follower)
                new_followers.extend(followings)
                for following in followings:
                    self.graph.add_edge(follower, following)
            followers = new_followers
            current_depth += 1

    @staticmethod
    def _get_following(username):
        url = GithubFollowGraph.FOLLOWING_URL.format(username)
        request = requests.get(url, auth=GithubFollowGraph.token)
        request.raise_for_status()
        following = [person['login'] for person in request.json()]
        return following

    def following(self):
        return list(self.graph.get_neighbours_for(self.main_user))

    def is_following(self, username):
        return username in self.following()

    def steps_to(self, username):
        last_level = [self.main_user]
        counter = 0
        while counter <= self.depth:
            next_level = set()
            if username in last_level:
                return counter
            for person in last_level:
                next_level = next_level.union(
                    self.graph.get_neighbours_for(person))
            last_level = next_level
            counter += 1
        return 0
Example #23
0
def transposeGraph(graph: DirectedGraph) -> DirectedGraph:
    new_graph = DirectedGraph()

    for vertex_id in graph.vertices:
        new_graph.addVertex(vertex_id, graph.getVertexLabel(vertex_id))

    for vertex_id in graph.vertices:
        for vertex_neighbor_id in graph.getVertexNeighbors(vertex_id):
            new_graph.addEdge(vertex_neighbor_id, vertex_id, graph.weight(vertex_id, vertex_neighbor_id))

    return new_graph
Example #24
0
 def __init__(self, username, depth):
     self.graph = DirectedGraph()
     self.main_user = username
     self.depth = depth
     followers = [self.main_user]
     current_depth = 0
     while current_depth < depth:
         new_followers = []
         for follower in followers:
             followings = GithubFollowGraph._get_following(follower)
             new_followers.extend(followings)
             for following in followings:
                 self.graph.add_edge(follower, following)
         followers = new_followers
         current_depth += 1
 def setUp(self):
     self.graph = DirectedGraph()
     self.graph.add_edge("Pesho", "Ivo")
     self.graph.add_edge("Ivan", "Ivo")
     self.graph.add_edge("Gosho", "Ivo")
     self.graph.add_edge("Ivan", "Pesho")
     self.graph.add_edge("Ivo", "Ivan")
Example #26
0
 def endpointsOfEdge(graph: DirectedGraph):
     '''
     Function that prints the endpoints of an edge
     '''
     edgeID = int(input("    edge id : "))
     print("The endpoints of edge with ID {0} are:".format(edgeID),
           graph.getEndpointsOf(edgeID), "\n")
Example #27
0
def main():
    '''
    Main program
    '''
    graph = DirectedGraph("graph1k.txt")
    #print(graph)
    cmdDict = {0: UI.exitApp,
               1: UI.nrOfVertices,
               2: UI.edgeBetween,
               3: UI.inOutDegree,
               4: UI.outboundEdges,
               5: UI.inboundEdges,
               6: UI.endpointsOfEdge,
               7: UI.retriveCost,
               8: UI.modifyCost,
               9: UI.addVertex,
               10: UI.removeVertex,
               11: UI.addEdge,
               12: UI.removeEdge,
               13: UI.saveGraph,
               14: UI.printMenu}

    UI.printMenu()
    UI.Print(graph)
    while True:
        try:
            cmd = UI.readCommand()
            cmdDict[cmd](graph)
        except Exception as ex:
            traceback.print_exc()
            print(ex)
Example #28
0
def create_random_graph(vertices, edges):
    """
    creates random graph with the number of vertices and edges given.
    :param vertices: number of vertices
    :param edges: number of edges
    :return:
    """
    graph = DirectedGraph(vertices)
    number_of_edges = 0
    while number_of_edges < edges:
        x = randint(0, vertices - 1)
        y = randint(0, vertices - 1)
        if not graph.is_edge_between(x, y):
            graph.add_edge(x, y, randint(0, 1000))
            number_of_edges += 1
    return deepcopy(graph)
Example #29
0
def deepFirstSearch(graph: DirectedGraph, init_vertex_id: str, visited: set) -> list:
    result_search = [init_vertex_id]
    visited.add(init_vertex_id)
    for vertex_id in graph.getVertexNeighbors(init_vertex_id):
        if vertex_id not in visited:
            result_search += deepFirstSearch(graph, vertex_id, visited)
    return result_search
Example #30
0
 def retriveCost(graph: DirectedGraph):
     '''
     Function that print the cost of an edge
     '''
     edgeID = int(input("    edge id : "))
     print("The cost of edge with ID {0} is:".format(edgeID),
           graph.getCostOf(edgeID), "\n")
Example #31
0
 def inboundEdges(graph: DirectedGraph):
     '''
     Function that prints the inbound edges of a vertex 
     '''
     vertex = int(input("    vertex : "))
     print("Inbound edges of {0}:".format(vertex),
           graph.inboundEdgesOf(vertex), "\n")
Example #32
0
def run():
    graph = DirectedGraph()
    #random_graph(graph, 100, 10)
    load_from_file(graph, "graph5.txt")
    service = Service(graph)
    ui = UI(graph, service)
    ui.run()
    save_to_file(graph, "graph_out.txt")
Example #33
0
def save_to_file(graph: DirectedGraph, file_name: str):
    with open(file_name, 'w') as data_file:
        output = f"{graph.vertices_count} {graph.edges_count}\n"

        for edge in graph.parse_edges():
            output += f"{edge.start} {edge.end} {edge.cost}\n"

        data_file.write(output)
def worker_task(i):
    global logger
    if logger is None:
        logging.basicConfig(
            format=
            "%(asctime)s [%(process)-4.4s--%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
        )
        fileHandler = logging.FileHandler('RD_log.log.{}'.format(os.getpid()),
                                          mode='w')
        logger = logging.getLogger()
        logger.addHandler(fileHandler)
        logger.setLevel(logging.DEBUG)
    random.seed(datetime.now())
    start_time = time.time()
    rounds = 10
    NODES = 7115
    min_edges = 75000
    max_edges = 125000
    incr = 0.001
    p = random.uniform(0.0015, 0.0024)  # probability
    seed = 100
    logger.info("# iteration {}".format(i + 1))
    graph = None
    edges, avgc = -1, -1
    while p > 0:
        logger.info("generating graph with N={} p={}".format(NODES, p))
        graph = DirectedGraph.randomDirectedGraph(NODES, p)
        edges = graph.size()[1]
        avgc = graph.toUndirect().average_clustering()
        logger.info("prob={} edges={}".format(p, edges))
        if edges >= min_edges and edges <= max_edges:
            break
            # avgc = graph.average_clustering()
            # logger.info("** avgc={}".format(avgc))
            # if avgc >= 0.1 and avgc <= 0.2:
            # 	break
            # elif avgc > 0.2:
            # 	p += ((max_edges - edges) / min_edges * 10.0) * incr
            # else:
            # 	p += ((min_edges - edges) / min_edges * 10.0) * incr
        elif edges > max_edges:
            p += ((max_edges - edges) / min_edges) * incr
        else:
            p += ((min_edges - edges) / min_edges) * incr
        sys.stdout.flush()

    #graph.setLogger(logger)

    ret = experiment(graph, seed, rounds)
    elapsed = time.time() - start_time
    ret.append((edges, avgc, elapsed))

    logger.info("# iteration %d done in %f" %
                (i + 1, time.time() - start_time))
    logger.info("# {}".format(ret))
    #gc.collect()
    #return [(lin_max_seed, max_lin_influenced), (eigenc_max_seed, max_eigenc_influenced), (bet_max_seed, max_bet_influenced)]
    return ret
Example #35
0
def deepFirstSearchWithInverseOrder(graph: DirectedGraph, initial_vertex: str,
                                    stack: list, visited: set):
    visited.add(initial_vertex)

    for vertex_id in graph.getVertexNeighbors(initial_vertex):
        if vertex_id not in visited:
            deepFirstSearchWithInverseOrder(graph, vertex_id, stack, visited)

    stack.insert(0, initial_vertex)
 def setUp(self):
     self.fr = DirectedGraph()
     self.fr.add_edge('sten', 'sho')
     self.fr.add_edge('sten', 'agito')
     self.fr.add_edge('sho', 'zina')
     self.fr.add_edge('sho', 'tom')
     self.fr.add_edge('tom', 'jerry')
     self.fr.add_edge('zina', 'agito')
     self.fr.add_edge('zina', 'sho')
 def setUp(self):
     self.graph = DirectedGraph()
     self.graph.add_edge("Pesho", "Tisho")
     self.graph.add_edge("Pesho", "Gosho")
     self.graph.add_edge("Tisho", "Grisho")
     self.graph.add_edge("Rafa", "Grisho")
     self.graph.add_edge("Grisho", "Tisho")
     self.graph.add_edge("Grisho", "Rafa")
     self.graph.get_neighbors_for("Grisho")
     self.graph.path_between("Pesho", "Rafa")
Example #38
0
class GitHubNetwork:
    def __init__(self, user_url):
        self.user_url = user_url

        self.user_network = DirectedGraph()
        self.user = User(user_url)
        self.user.make_followers()
        self.user.make_following()

    def build_network(self, user_url, level):
        current_level = 0

        visited = set()
        visited.add(self.user)

        queue = []
        queue.append((current_level, self.user), )

        while len(queue) != 0:
            current_level, current_user = queue.pop(0)

            if current_level + 1 > level:
                break

            current_user.make_followers()
            current_user_followers = current_user.get_followers()

            current_user.make_following()
            current_user_following = current_user.get_following()

            for follower in current_user_followers:
                if follower not in visited:
                    self.user_network.add_edge(current_user, follower)
                    queue.append((current_level + 1, follower), )
                    visited.add(follower)

            for following in current_user_following:
                if following not in visited:
                    self.user_network.add_edge(following, current_user)
                    queue.append((current_level + 1, follower), )
                    visited.add(follower)

    def do_you_follow(self, user_followed):
        return self.user_network.edge_between(self.user, user_followed)

    def do_you_follow_indirectly(self, user_following):
        return self.user_network.path_between(self.user, user_following)

    def does_he_she_follows(self, user_following):
        self.user.make_following()
        if user_following.login in self.user.get_following():
            return True
        else:
            return False

    def does_he_she_follow_indirectly(self, user_following):
        return self.user_network.path_between(user_following, self.user)
def worker_task(i):
	global logger
	if logger is None:
		logging.basicConfig(format="%(asctime)s [%(process)-4.4s--%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
		fileHandler = logging.FileHandler('RD_log.log.{}'.format(os.getpid()),mode='w')
		logger=logging.getLogger()
		logger.addHandler(fileHandler)
		logger.setLevel(logging.DEBUG)
	random.seed(datetime.now())	
	start_time = time.time()
	rounds = 10
	NODES = 7115
	min_edges = 75000
	max_edges = 125000
	incr = 0.001
	p = random.uniform(0.0015, 0.0024) # probability
	seed = 100
	logger.info("# iteration {}".format(i+1))
	graph = None
	edges, avgc = -1, -1
	while p > 0:
		logger.info("generating graph with N={} p={}".format(NODES, p))
		graph = DirectedGraph.randomDirectedGraph(NODES, p)
		edges = graph.size()[1]
		avgc = graph.toUndirect().average_clustering()
		logger.info("prob={} edges={}".format(p, edges))
		if edges >= min_edges and edges <= max_edges:
			break
			# avgc = graph.average_clustering()
			# logger.info("** avgc={}".format(avgc))
			# if avgc >= 0.1 and avgc <= 0.2:
			# 	break
			# elif avgc > 0.2:
			# 	p += ((max_edges - edges) / min_edges * 10.0) * incr
			# else:
			# 	p += ((min_edges - edges) / min_edges * 10.0) * incr
		elif edges > max_edges:
			p += ((max_edges - edges) / min_edges) * incr
		else:
			p += ((min_edges - edges) / min_edges) * incr
		sys.stdout.flush()

	#graph.setLogger(logger)
	
	ret = experiment(graph, seed, rounds)
	elapsed = time.time() - start_time
	ret.append((edges, avgc, elapsed))
	
	logger.info("# iteration %d done in %f" % (i+1, time.time() - start_time))
	logger.info("# {}".format(ret))
	#gc.collect()
	#return [(lin_max_seed, max_lin_influenced), (eigenc_max_seed, max_eigenc_influenced), (bet_max_seed, max_bet_influenced)]
	return ret
Example #40
0
def topologialSort(graph: DirectedGraph) -> str:
    stack = list()
    visited = set()

    for vertex_id in graph.vertices:
        if vertex_id not in visited:
            deepFirstSearchWithInverseOrder(graph, vertex_id, stack, visited)

    stack_labels = list()
    for vertex_id in stack:
        stack_labels.append(graph.getVertexLabel(vertex_id))

    return ' -> '.join(stack_labels) + '\n'
Example #41
0
def load_activities(graph: DirectedGraph, file_name: str):
    with open(file_name, 'r') as data_file:
        first_line = data_file.readline()
        activity_count = first_line
        activity_count = int(activity_count)

        for i in range(activity_count + 1):
            graph.add_vertex(i)

        for i, line in enumerate(data_file):
            (prerequisites_str, duration) = line.split(' ')

            prerequisites_str = prerequisites_str.split(',')
            prerequisites = []

            for prerequisite_str in prerequisites_str:
                prerequisites.append(int(prerequisite_str))

            duration = int(duration)

            for prerequisite in prerequisites:
                graph.add_edge(prerequisite, i + 1, duration)
def buildGraphFromFile(file_path: str, is_directed: bool = False):
	graph_file = open(file_path, 'r')

	if is_directed:
		graph = DirectedGraph()
	else:
		graph = NotDirectedGraph()

	graph_file.readline() #ignoring first line

	build_vertices = True
	for line in graph_file:
		line = line.rstrip("\n\r").rstrip(' ') #remove new line character and spaces in the end of the line

		if build_vertices:
			if line in {"*edges"} or line in {"*arcs"}:
				build_vertices = False
			else:
				try:
					splittedLine = line.split(' ', 1)

					if len(splittedLine) == 2:
						vertexId = splittedLine[0]
						vertexName = splittedLine[1].replace("\"", "")

						graph.addVertex(vertexId, vertexName)
					else:
						raise Exception("Error while building vertex (graph = "+file_path+"): \""+line+"\" isn't in the default template: (id label)")

				except Exception as error:
					print("(graph = "+file_path+"):"+str(error))
					return NotDirectedGraph()
		else:
			try:
				splittedLine = line.split(' ')

				if len(splittedLine) == 3:
					origin_vertex_id = splittedLine[0]
					destiny_vertex_id = splittedLine[1]
					edge_weight = float(splittedLine[2])

					graph.addEdge(origin_vertex_id, destiny_vertex_id, edge_weight)
				else:
					raise Exception("Error while building edge (graph = "+file_path+"): \""+line+"\" isn't in the default template: (id id weight)")
			except ValueError:
				print("Error while building edge (graph = "+file_path+"): \""+splittedLine[2]+"\" (your weight) isn't a float")
				return NotDirectedGraph()
			except Exception as error:
				print("(graph = "+file_path+"): "+str(error))
				return NotDirectedGraph()

	return graph
Example #43
0
class GitHubSocialNetwork:

    def __init__(self, user):
        self.user = user
        self.graph = DirectedGraph()

    def __followers_caller(self, user):
        client_id = "f8be3d072416ea05b379"
        client_secret = "63f1f862a9c6c2b892dcc577f462b1cb0f095722"
        data = 'https://api.github.com/users/{}/following'.format(user)
        data += '?client_id={}&client_secret={}'.format(client_id, client_secret)
        return data

    def __graph_level_builder(self, user, graph):
        r = requests.get(self.__followers_caller(user))
        for follower in json.loads(r.text):
            graph.add_edge(user, follower["login"])

    def __graph_builder(self, user, depth, graph, max_depth):
        if (depth > max_depth):
            return
        self.__graph_level_builder(user, graph)
        for follower in graph.graph[user]:
            self.__graph_builder(follower, depth + 1, graph, max_depth)

    def __graph_init(self, max_depth):
        if (max_depth >= 4):
            raise ValueError("So Deep")
        self.__graph_builder(self.user, 0, self.graph, max_depth)

    def following(self):
        graph = DirectedGraph()
        self.__graph_level_builder(self.user, graph)
        return graph.graph[self.user]

    def is_following(self, followed):
        return followed in self.following()

    def steps_to(self, username):
        self.__graph_init(3)
        return self.graph.steps_between(self.user, username)
Example #44
0
def EM_run_8():
	print "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	print "Experiment run 8: [tt, tf, ft, ff] = [0.9, 0.6, 0.3, 0.2]"
	print "Multiple recovery"
	print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	graph = DirectedGraph(5, [0.5 ,0.5])
	graph.draw_edge(0, 2)
	graph.draw_edge(1, 2)
	graph.draw_edge(2, 3)
	graph.draw_edge(2, 4)
	
	graph.init_CPT()
	graph.set_node_est_CPT(0, [0.1])
	graph.set_node_est_CPT(1, [0.2])
	graph.set_node_est_CPT(2, [0.2, 0.3, 0.6, 0.9])
	#graph.set_node_est_CPT(2, [0.1, 0.1, 0.2, 0.2])
	graph.set_node_est_CPT(3, [0.2, 0.9])
	graph.set_node_est_CPT(4, [0.1, 0.8])
	
	
	
	'''
	samples = []
	for i in range(100):
		curr_sample = []
		for j in range(5):
			curr_sample.append(round(random.random(), 0))
		samples.append(curr_sample)
	
	samples = np.array(samples, dtype=np.int64)
	'''
	samples = np.array([[0, 0, 0, 0, 0],
											[0, 0, 0, 1, 0],
											[1, 0, 0, 1, 1],
											[0, 0, 0, 0, 1],
											[0, 1, 1, 1, 0],
											[0, 0, 0, 0, 1],
											[1, 1, 0, 1, 1],
											[0, 0, 0, 0, 0],
											[0, 0, 1, 1, 0],
											[0, 0, 0, 0, 1]])
	graph.EM(samples, 2, 10000)
	graph.EM(samples, 3, 10000)
Example #45
0
def EM_run_9():
	print "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	print "Experiment run 9: [tt, tf, ft, ff] = [0.9, 0.6, 0.3, 0.2]"
	print "RANDOM DATA - Varying Sample size (double)"
	print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	
	iters = 0
	simulations = 1000
	runs = []

	for i in range(simulations):
		graph = DirectedGraph(5, [0.5 ,0.5])
		graph.draw_edge(0, 2)
		graph.draw_edge(1, 2)
		graph.draw_edge(2, 3)
		graph.draw_edge(2, 4)
		
		graph.init_CPT()
		graph.set_node_est_CPT(0, [0.1])
		graph.set_node_est_CPT(1, [0.2])
		graph.set_node_est_CPT(2, [0.2, 0.3, 0.6, 0.9])
		graph.set_node_est_CPT(3, [0.2, 0.9])
		graph.set_node_est_CPT(4, [0.1, 0.8])

		samples = []
		for i in range(100):
			curr_sample = []
			for j in range(5):
				curr_sample.append(round(random.random(), 0))
			samples.append(curr_sample)
		
		samples = np.array(samples, dtype=np.int64)
		
		#print "RANDOM DATA"
		#print str(samples) 	

		ret = graph.EM(samples, 2, 1000)
		iters += ret
		runs.append(ret)
	
	print "Took approximately: " + str(iters/simulations)
	plt.figure()
	plt.hist(runs, bins=50)
	plt.title('Distribution of Iterations Until Converged')
	plt.xlabel('Number of Iterations')
	plt.ylabel('Frequency')
	plt.savefig('dist_9.png')
	print "Took approximately: " + str(iters/simulations)
 def setUp(self):
     self.new_graph = DirectedGraph()
Example #47
0
def EM_run_6b():
	
	#print "\n Experiment run 6: [tt, tf, ft, ff] = [0.9, 0.6, 0.3, 0.2]"
	#print "RANDOM DATA"
	
	iters = 0
	simulations = 10000
	

	graph = DirectedGraph(5, [0.5 ,0.5])
	graph.draw_edge(0, 2)
	graph.draw_edge(1, 2)
	graph.draw_edge(2, 3)
	graph.draw_edge(2, 4)
	
	graph.init_CPT()
	graph.set_node_est_CPT(0, [0.1])
	graph.set_node_est_CPT(1, [0.2])
	graph.set_node_est_CPT(2, [0.2, 0.3, 0.6, 0.9])
	graph.set_node_est_CPT(3, [0.2, 0.9])
	graph.set_node_est_CPT(4, [0.1, 0.8])

	samples = []
	for i in range(100):
		curr_sample = []
		for j in range(5):
			curr_sample.append(round(random.random(), 0))
		samples.append(curr_sample)
	
	samples = np.array(samples, dtype=np.int64)
	
	#print "RANDOM DATA"
	#print str(samples) 

	return graph.EM(samples, 2, 10000)
 def __init__(self):
     self.dict_of_following = {}
     self.list_of_follwoers_names = []
     self.dg = DirectedGraph()
class DirectedClassTest(unittest.TestCase):
    def setUp(self):
        self.new_graph = DirectedGraph()

    def test_init(self):
        self.assertEqual(self.new_graph.graph, {})

    def test_add_edge(self):
        self.new_graph.add_edge("Ivan", "Petar")
        self.new_graph.add_edge("Petar", "Georgi")
        self.assertEqual(self.new_graph.graph, {"Ivan": ["Petar"], "Petar": ["Georgi"], "Georgi": []})

    def test_get_neighbours(self):
        self.new_graph.add_edge("Ivan", "Petar")
        self.new_graph.add_edge("Petar", "Georgi")
        self.assertEqual(self.new_graph.get_neighbours_for("Ivan"), ["Petar"])
        self.assertEqual(self.new_graph.get_neighbours_for("Petar"), ["Georgi"])

    def test_path_between_direct_neighbours(self):
        self.new_graph.add_edge("Ivan", "Petar")
        self.assertTrue(self.new_graph.path_between("Ivan", "Petar"))

    def test_path_between_false(self):
        self.new_graph.add_edge("Ivan", "Petar")
        self.new_graph.add_edge("Valeri", "Georgi")
        self.assertFalse(self.new_graph.path_between("Ivan", "Dragan"))
        self.assertFalse(self.new_graph.path_between("Ivan", "Georgi"))

    def test_path_between_distant_neighbour(self):
        self.new_graph.add_edge("Ivan", "Petar")
        self.new_graph.add_edge("Petar", "Georgi")
        self.assertTrue(self.new_graph.path_between("Ivan", "Georgi"))

    def test_path_between_cycle(self):
        self.new_graph.add_edge("Ivan", "Petar")
        self.new_graph.add_edge("Petar", "Ivan")
        self.assertTrue(self.new_graph.path_between("Petar", "Ivan"))

    def test_path_between_cycles(self):
        self.new_graph.add_edge("1", "2")
        self.new_graph.add_edge("2", "1")
        self.new_graph.add_edge("2", "3")
        self.new_graph.add_edge("3", "2")
        self.new_graph.add_edge("3", "1")
        self.new_graph.add_edge("2", "4")
        self.new_graph.add_edge("4", "2")
        self.new_graph.add_edge("4", "3")
        self.new_graph.add_edge("4", "1")
        self.new_graph.add_edge("3", "5")
        self.assertTrue(self.new_graph.path_between("1", "5"))
    def setUp(self):
        self.node_one = Node('Penko')
        self.node_two = Node('Penka')

        self.graph = DirectedGraph()
Example #51
0
 def test_edge_added_directed(self):
     graph = DirectedGraph(7)
     graph.add_edge(2, 9)
     self.assertEqual(graph.edge, 0)
     self.assertEqual(graph.adj_list[2], [])
     self.assertNotEqual(len(graph.adj_list), 9)
     graph.add_edge(0, 1)
     graph.add_edge(0, 5)
     graph.add_edge(6, 0)
     graph.add_edge(6, 4)
     graph.add_edge(2, 3)
     self.assertEqual(graph.adj_list[2], [3])
     self.assertEqual(graph.adj_list[3], [])
     graph.add_edge(3, 2)
     self.assertEqual(graph.adj_list[2], [3])
     self.assertEqual(graph.adj_list[3], [2])
     graph.add_edge(2, 0)
     graph.add_edge(3, 5)
     graph.add_edge(4, 2)
     graph.add_edge(4, 3)
     graph.add_edge(5, 4)
     self.assertEqual(graph.edge, 11)
class TestDirectedGraph(unittest.TestCase):
    def setUp(self):
        self.node_one = Node('Penko')
        self.node_two = Node('Penka')

        self.graph = DirectedGraph()

    def test_add_edge(self):
        self.graph.add_edge(self.node_one, self.node_two)
        self.assertIn(self.node_one, self.graph.nodes[self.node_two.name].is_pointed_by)
        self.assertIn(self.node_two, self.graph.nodes[self.node_one.name].points_to)

    def test_get_neighbours_for(self):
        pencho = Node('Pencho')

        self.graph.add_edge(self.node_one, self.node_two)
        self.graph.add_edge(self.node_one, pencho)
        self.assertEqual([self.node_two, pencho], self.graph.get_neighbours_for(self.node_one))

    def test_path_between_normal(self):
        self.graph.add_edge(self.node_one, self.node_two)
        self.assertTrue(self.graph.path_between(self.node_one, self.node_two))
        self.assertFalse(self.graph.path_between(self.node_two, self.node_one))

    def test_path_between_rare(self):
        penichka = Node('Penichka')
        self.graph.add_edge(self.node_one, self.node_two)
        self.graph.add_edge(self.node_two, penichka)
        self.assertTrue(self.graph.path_between(self.node_one, penichka))

    def test_path_between_recursion(self):
        uno = Node('1')
        dos = Node('2')
        tres = Node('3')
        quatro = Node('4')
        cinco = Node('5')
        seis = Node('6')

        self.graph.add_edge(uno, dos)
        self.graph.add_edge(dos, tres)
        self.graph.add_edge(dos, quatro)
        self.graph.add_edge(quatro, cinco)
        self.graph.add_edge(quatro, seis)
        self.graph.add_edge(tres, dos)

        self.assertTrue(self.graph.path_between(uno, seis))
class TestGraph(unittest.TestCase):

    def setUp(self):
        self.graph = DirectedGraph()
        self.graph.add_edge("Ivo", "Hrisi")
        self.graph.add_edge("Pesho", "Ivo")
        self.graph.add_edge("Pesho", 'a')
        self.graph.add_edge("Niki", "Georgi")

    def test_addEdge(self):
        self.assertIn(("Niki", "Georgi"), self.graph.edges)
        self.assertIn("Niki", self.graph.nodes)
        self.assertIn("Georgi", self.graph.nodes)

    def test_get_neighbours(self):
        self.assertIn("a", self.graph.get_neighbors_for("Pesho"))
        self.assertIn("Ivo", self.graph.get_neighbors_for("Pesho"))
        self.assertIn("Hrisi", self.graph.get_neighbors_for('Ivo'))

    def test_path_between(self):
        self.assertTrue(self.graph.path_between("Ivo", "Hrisi"))
        self.assertFalse(self.graph.path_between("Hrisi", "Ivo"))
        self.assertTrue(self.graph.path_between("Niki", "Georgi"))
        self.assertTrue(self.graph.path_between("Pesho", "Hrisi"))

    @unittest.skip("Just testing print")
    def test_print(self):
        print(self.graph)
        self.assertTrue(False)
 def setUp(self):
     self.graph = DirectedGraph()
     self.graph.add_edge("Ivo", "Hrisi")
     self.graph.add_edge("Pesho", "Ivo")
     self.graph.add_edge("Pesho", 'a')
     self.graph.add_edge("Niki", "Georgi")
Example #55
0
 def test_out_degree_directed(self):
     graph = DirectedGraph(7)
     graph.add_edge(0, 1)
     graph.add_edge(0, 5)
     self.assertEqual(graph.out_degree(0), 2)
class DirectedGraphTests(unittest.TestCase):
    def setUp(self):
        self.graph = DirectedGraph()
        self.graph.add_edge("Pesho", "Ivo")
        self.graph.add_edge("Ivan", "Ivo")
        self.graph.add_edge("Gosho", "Ivo")
        self.graph.add_edge("Ivan", "Pesho")
        self.graph.add_edge("Ivo", "Ivan")

    def test_add_edge(self):
        self.assertEqual(len(self.graph.graph), 4)

    def test_get_neighbors(self):
        self.assertEqual(len(self.graph.graph['Ivan']), 2)

    def test_path_between(self):
        self.assertTrue(self.graph.path_between("Pesho", "Ivo"))
        self.assertTrue(self.graph.path_between("Ivo", "Pesho"))
        self.assertFalse(self.graph.path_between("Ivan", "Gosho"))

    def test_steps_between(self):
        self.assertEqual(self.graph.steps_between("Pesho", "Pesho"), 0)
        self.assertEqual(self.graph.steps_between("Ivan", "Pesho"), 1)
        self.assertEqual(self.graph.steps_between("Pesho", "Ivan"), 2)
        self.assertEqual(self.graph.steps_between("Gosho", "Pesho"), 3)

    def test_to_string(self):
        test_string = str(self.graph)
        self.assertEqual(len(test_string), 71)
Example #57
0
def EM_run_3():
	print "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	print "Experiment run 3: [tt, tf, ft, ff] = [0.9, 0.9, 0.9, 0.9]"
	print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"	
	graph = DirectedGraph(5, [0.5 ,0.5])
	graph.draw_edge(0, 2)
	graph.draw_edge(1, 2)
	graph.draw_edge(2, 3)
	graph.draw_edge(2, 4)
	
	graph.init_CPT()
	graph.set_node_est_CPT(0, [0.1])
	graph.set_node_est_CPT(1, [0.2])
	graph.set_node_est_CPT(2, [0.9, 0.9, 0.9, 0.9])
	graph.set_node_est_CPT(3, [0.2, 0.9])
	graph.set_node_est_CPT(4, [0.1, 0.8])

	samples = np.array([[0, 0, 0, 0, 0],
											[0, 0, 0, 1, 0],
											[1, 0, 0, 1, 1],
											[0, 0, 0, 0, 1],
											[0, 1, 0, 1, 0],
											[0, 0, 0, 0, 1],
											[1, 1, 0, 1, 1],
											[0, 0, 0, 0, 0],
											[0, 0, 0, 1, 0],
											[0, 0, 0, 0, 1]])

	graph.EM(samples, 2, 100)
Example #58
0
 def test_max_degree_directed(self):
     graph = DirectedGraph(7)
     graph.add_edge(0, 1)
     graph.add_edge(2, 1)
     self.assertEqual(graph.max_degree(), 2)