Exemple #1
0
    def getAllSocialPaths(self, userID):
        """
        Takes a user's userID as an argument

        Returns a dictionary containing every user in that user's
        extended network with the shortest friendship path between them.

        The key is the friend's ID and the value is the path.
        """
        visited = {}  # Note that this is a dictionary, not a set
        # !!!! IMPLEMENT ME

        newGraph = Graph()
        for user in self.users:
            newGraph.add_vertex(user)

        for user in self.friendships:
            for friend in self.friendships[user]:
                newGraph.add_edge(user, friend)

        for friend in self.users:
            socialPath = newGraph.bfs(userID, friend)
            if socialPath is not False:
                visited[friend] = socialPath

        return visited
Exemple #2
0
def testGraph():
    '''
    Here we test algorithms for graphs
    We test 
     a) breadth first search
     b) depth first search
     c) minimum spanning tree using Prim's algorithm
     d) shortest distance using Dijkstra's algorithm
    
    Graph's vertices are specified as a list
    Graph's edges are represented as a dictionary
    '''

    print('Create a default graph')
    g = Graph()
    print(g.E)

    print('')
    print('Changing the default graph to the following')
    g.V = [str(i) for i in range(9)]
    g.E = {
        '0': {'1':4, '7':8},
        '1': {'0':4, '2':8, '7':11},
        '2': {'1':8, '3':7, '5':4, '8':2},
        '3': {'2':7, '4':9, '5':14},
        '4': {'3':9, '5':10},
        '5': {'2':4, '3':14, '4':10, '6':2},
        '6': {'5':2, '7':1, '8':6},
        '7': {'0':8, '1':11, '6':1, '8':7},
        '8': {'2':2, '6':6, '7':7}
    }
    print(g.E)

    strt = '0'

    print('\nBFS with starting node ' + strt)
    g.bfs(strt)

    print('\nDFS with starting node ' + strt)
    g.dfs(strt)

    print('\nPrim-MST with starting node '+ strt)
    g.prim_mst(strt)

    print('\nDijkstra with starting node ' + strt)
    g.dijkstra(strt)
Exemple #3
0
    def getAllSocialPaths(self, userID):
        """
        Takes a user's userID as an argument

        Returns a dictionary containing every user in that user's
        extended network with the shortest friendship path between them.

        The key is the friend's ID and the value is the path.
        """
        visited = {}  # Note that this is a dictionary, not a set
        # !!!! IMPLEMENT ME

        # With the Graph class
        # runtime: 1.9708278179168701 seconds 100 users, 20 average
        # runtime: 24.768869876861572 seconds 200 users, 20 average
        # runtime: 24.120848417282104 seconds
        # runtime: 24.88981318473816 seconds
        g = Graph()
        for user in self.users:
            g.add_vertex(user)

        for user in self.friendships:
            for friend in self.friendships[user]:
                g.add_edge(user, friend)

        for friend in self.users:
            path = g.bfs(userID, friend)
            if path is not False:
                visited[friend] = path

        # Without the Graph class but have Queue
        # runtime: 1.8722269535064697 seconds
        # runtime: 27.13098406791687 seconds
        # runtime: 26.577613592147827 seconds
        # runtime: 26.608980178833008 seconds
        # for friend in self.users:
        #     q = Queue()
        #     visit = set()
        #     path = []
        #     q.enqueue([userID])

        #     while len(q.storage) > 0:
        #         node = q.dequeue()
        #         path = node
        #         vnode = node[-1]
        #         if vnode == friend:
        #             visited[friend] = path
        #             pass
        #         visit.add(vnode)
        #         for child in self.friendships[vnode]:
        #             if child not in visit:
        #                 dup_node = node[:]
        #                 dup_node.append(child)
        #                 q.enqueue(dup_node)

        return visited