#Understanding any algorithm with ChatGPT

3 messages · Page 1 of 1 (latest)

frank quarry
#

Here is a prompt that I used to understand and visualise an algorithm that I saw in class yesterday. I generalize it and I want to share my work with everyone:

General Prompt:
You are tasked with visualizing the execution of a graph algorithm using a plotting library. Follow the instructions below to complete the task:

[Algorithm Name]: Choose an appropriate name algorithm

[Steps of the Algorithm]: Specify the individual steps involved in the algorithm's execution. For each step, describe the necessary operations or decisions made.

[Display Specification]: Select the plotting library or method to use for visualizing the graph and the algorithm's execution provided. In general, the user will give you matplotlib or manim.

[Additional Rules of the Algorithm]: Define any extra rules or conditions that should be considered during the algorithm's execution.

Your task is to write Python code that implements the chosen algorithm, generates a corresponding graph, and visualizes the algorithm's execution based on the provided steps and display specifications. Ensure that you adhere to any additional rules of the algorithm, if specified.

You have two options to proceed:

[Random graph generated]: Write the code to generate a random graph for the algorithm's execution.
[My own graph]: Provide your own graph representation as input for the algorithm.

#

Here is the specific prompt used:

You are tasked with visualizing the execution of a graph algorithm using a plotting library. Follow the instructions below to complete the task:

[Algorithm Name] :=  Kruskal's algorithm

[Steps of the Algorithm] := 
1. First, each vertex of G is a single tree ⇒ G looks like a forest of n trees.
2. Sort the edges of G in increasing order, based on their weights.
3. Pick the edges of G in order, and for each of them, if their vertices belong
to different trees merge the two trees to build a larger tree.
4. At the end, the forest should contain only one (very large) tree, which is a
MST of G.

[Display Specification]:= The plotting library selected to visualzing each step of the algorithm is matplotlib.

[Additional Rules of the Algorithm]:= Your code plot cases where an edge is skipped due to it causing a cycle in the MST. You could plot the same graph with the tilte: the edge (?;?) would create a cycle.

[Random graph generated]: Write the code to generate a random weighted graph  of 6 vertices for the algorithm's execution

Your task is to write Python code that implements the chosen algorithm, generates a corresponding graph, and visualizes the algorithm's execution based on the provided steps and display specifications. Ensure that you adhere to any additional rules of the algorithm, if specified.
#

After I only asked him to "merge all the plots into a single image whic can be achieved by using subplots in matplotlib and save the image."