Beginner Here - Help w/ Programming Assignment: Implementing algorithms in Graphs

Greetings!

Hope you all are well.

I am a New Born to the Computer Science/AI community, and recently gained a huge interest in this field. I was wondering if there were others like me who could use a study/accountability partner for the course. Any expert who has time to offer their friendly guidance would also be awesome also. Currently, I am struggling with the Graphs assignment and was hoping to partner with others for support. The LLM is super helpful, but there is nothing like having a study buddy.

If anyone would like to connect, that would be very helpful and appreciated.

Hi @Jemyle,

Although I can’t act as your study buddy, I’m still able to assist you with your questions. Please feel free to post them here, and I’ll provide the help you need. Just keep in mind that posting direct solutions is not allowed.

Best,
Lucas

Hi there @lucas.coutinho !

Thank you for your response.

I am finding that when I request that the LLM not modify certain parts of the code, it will do so in its first response. However, as I continue to communicate with the LLM it will basically go back slip in edits of those same portions.

Also, I have to start a new conversation after every 3-4 request, which I may contribute to my first issue. I feel as if may be sending myself through circles. :sweat_smile:

I was hoping that others could provide tips to help make this process easier.

Hi @lucas.coutinho
I have been running a test for the tsp_medium_graph function with a few updates and getting the below error:
Failed test case: Exceeded time execution limit for a tour starting in node 0. To replicate the graph, you may run generate_graph(nodes = 300, complete = True, seed = 42).
Expected: tsp_medium_graph method must run in less than 1.3 seconds
Got: Time execution exceeded 1.3 seconds
The latest approach uses a Modified Nearest Neighbor Heuristic with Greedy Improvements.
Any recommendations on how to deal with that? - thank you

Hi @GaryKom!

I’d suggest asking an LLM to find possible bottlenecks in your code and then asking it to improve them.

Hi Lucas,

I’m also a beginner here, and I’ve been having problems with the unit test. Except for the tsp_small_graph_function, the other 3 test failed. I tried to solved this with LLM multiple times but I suspect I might be running in circle at this point. Can you explain why this Grader output error happen across all 3 other tests?

Failed test case: Failed to achieve 85% of success in test cases. Examples of failed tests will be provided.
Expected:
At least 85% of success shortest_path running below 0.7s and achieving the correct distance,
but got:
Success rate: 0%.

Failed test case: shortest_path returned an exception in test case. You may replicate the graph by running generate_graph(nodes = 10000, edges = 10, seed = 65).
Expected:
shortest_path must run without exceptions,
but got:
Exception thrown is: cannot unpack non-iterable int object.

Failed test case: shortest_path returned an exception in test case. You may replicate the graph by running generate_graph(nodes = 10000, edges = 10, seed = 66).
Expected:
shortest_path must run without exceptions,
but got:
Exception thrown is: cannot unpack non-iterable int object.

Failed test case: shortest_path returned an exception in test case. You may replicate the graph by running generate_graph(nodes = 10000, edges = 10, seed = 67).
Expected:
shortest_path must run without exceptions,
but got:
Exception thrown is: cannot unpack non-iterable int object.

Thank you

Hi @vnsavitri

Given the exception, it looks like the return of your function is not in the format dist, path where dist is the distance and path is a list with the nodes.

Can you please doublecheck every return statement in your function to check if it meets the requirement?

Cheers,
Lucas

Thanks, Lucas. I’ll have a look at this.

Hi @lucas.coutinho

I’m stuck on this assignment, as well. Below is my code for the shortest_path function and below is the error I’m getting. Any help would be much appreciated!

class Graph_Advanced(Graph):
    
    def shortest_path(self, start, end): 
        """
        Calculate the shortest path from a starting node to an ending node in a sparse graph
        with potentially 10000s of nodes. Must run under 0.5 seconds and find the shortest distance between two nodes.
        
        Parameters:
        start: The starting node.
        end: The ending node.
        
        Returns:
        A tuple containing the total distance of the shortest path and a list of nodes representing that path.
        """
        # Your code here
        # Priority queue to hold nodes to explore, initialized with the start node
        priority_queue = [(0, start, [])]  # (current_distance, current_node, current_path)
        # Dictionary to keep track of the shortest distance to each node
        shortest_distances = {start: 0}
        # Set to keep track of visited nodes
        visited = set()
        
        while priority_queue:
            # Get the node with the smallest distance
            current_distance, current_node, path = heapq.heappop(priority_queue)
            
            # If the node has been visited, skip it
            if current_node in visited:
                continue
            
            # Add the current node to the visited set
            visited.add(current_node)
            
            # Append the current node to the path
            path = path + [current_node]
            
            # If we have reached the end node, return the distance and path
            if current_node == end:
                dist = current_distance
                return dist, path
            
            # Explore neighbors
            for neighbor, weight in self.get(current_node, {}).items():
                if neighbor not in visited:
                    old_cost = shortest_distances.get(neighbor, float('inf'))
                    new_cost = current_distance + weight
                    if new_cost < old_cost:
                        shortest_distances[neighbor] = new_cost
                        heapq.heappush(priority_queue, (new_cost, neighbor, path))
        
        # If the end node is unreachable, return infinity and an empty path
        dist = float('inf')
        path = []
        return dist, path
        # End here

Error:

NameError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 class Graph_Advanced(Graph):
      3     def shortest_path(self, start, end): 
      4         """
      5         Calculate the shortest path from a starting node to an ending node in a sparse graph
      6         with potentially 10000s of nodes. Must run under 0.5 seconds and find the shortest distance between two nodes.
   (...)
     13         A tuple containing the total distance of the shortest path and a list of nodes representing that path.
     14         """

NameError: name 'Graph' is not defined

Hi @anceBal,

It looks like you moved the Graph implementation from the initial cell to another one, or deleted the initial cell containing the Graph implementation. Can you please refresh your workspace and add your solution in the new assignment notebook, without changing where the codes are placed?

Let me know if this helps.

Thanks,
Lucas

Hi @lucas.coutinho ,

I refreshed my workspace, per your suggestion, but unfortunately I’m still running into the same exact error.

@anceBal

Can you please send me your notebook privately?

Cheers,
Lucas