File:Loop erased random walk in 2D.png
Original file (1,600 × 1,600 pixels, file size: 176 KB, MIME type: image/png)
Captions
Summary
[edit]DescriptionLoop erased random walk in 2D.png |
English: ```python
import random def random_walk(N): """ Simulate a random walk on the integer grid for N steps. """ path = [(0, 0)] # starting point for _ in range(N): x, y = path[-1] # Randomly choose a direction: up, down, left, or right direction = random.choice([(0, 1), (0, -1), (1, 0), (-1, 0)]) next_step = (x + direction[0], y + direction[1]) path.append(next_step) return path def loop_erasure(path): """ Two-pass loop erasure algorithm. First pass: Record the latest occurrence of each point in a dictionary. Second pass: Use the dictionary to skip over points leading into loops. """ latest_occurrence = {} # First Pass: Record the latest occurrence of each point for i, point in enumerate(path): latest_occurrence[point] = i erased_path = [] i = 0 # Second Pass: Use the dictionary to skip over points leading into loops while i < len(path): point = path[i] erased_path.append(point) i = latest_occurrence[point] + 1 return erased_path import matplotlib.pyplot as plt def plot_paths(path, erased_path): """ Plot the given path and its loop-erased subpath. """ # Unzip the coordinates for plotting x_path, y_path = zip(*path) x_erased, y_erased = zip(*erased_path) plt.figure(figsize=(16,16)) # Plot the original path in black plt.plot(x_path, y_path, color='black', label='Original Path') # Plot the erased path in red plt.plot(x_erased, y_erased, color='red', linestyle='--', label='Erased Path') # Set aspect ratio to 1:1 plt.gca().set_aspect('equal', adjustable='box') # Remove gridlines and axes plt.grid(False) plt.axis('off') plt.savefig(f"loop_erasure_{N}.png") N = 1000000 path = random_walk(N) erased_path = loop_erasure(path) plot_paths(path, erased_path) ``` |
Date | |
Source | Own work |
Author | Cosmia Nebula |
Licensing
[edit]- You are free:
- to share – to copy, distribute and transmit the work
- to remix – to adapt the work
- Under the following conditions:
- attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
File history
Click on a date/time to view the file as it appeared at that time.
Date/Time | Thumbnail | Dimensions | User | Comment | |
---|---|---|---|---|---|
current | 05:07, 25 November 2023 | 1,600 × 1,600 (176 KB) | Cosmia Nebula (talk | contribs) | Uploaded while editing "Loop-erased random walk" on en.wikipedia.org |
You cannot overwrite this file.
File usage on Commons
There are no pages that use this file.
File usage on other wikis
The following other wikis use this file:
- Usage on en.wikipedia.org
Metadata
This file contains additional information such as Exif metadata which may have been added by the digital camera, scanner, or software program used to create or digitize it. If the file has been modified from its original state, some details such as the timestamp may not fully reflect those of the original file. The timestamp is only as accurate as the clock in the camera, and it may be completely wrong.
Software used | |
---|---|
Horizontal resolution | 39.37 dpc |
Vertical resolution | 39.37 dpc |