File:Loop_erased_random_walk_in_2D.png
From Wikipedia, the free encyclopedia
Size of this preview: 600 × 600 pixels. Other resolutions: 240 × 240 pixels | 480 × 480 pixels | 768 × 768 pixels | 1,024 × 1,024 pixels | 1,600 × 1,600 pixels.
Original file (1,600 × 1,600 pixels, file size: 176 KB, MIME type: image/png)
This is a file from the Wikimedia Commons. Information from its description page there is shown below. Commons is a freely licensed media file repository. You can help. |
Summary
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
I, the copyright holder of this work, hereby publish it under the following license:
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
- 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.
Items portrayed in this file
depicts
image/png
7ac2721cb6f28b809735327e9296cb2398c0cc0f
180,094 byte
1,600 pixel
1,600 pixel
24 November 2023
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 | Uploaded while editing "Loop-erased random walk" on en.wikipedia.org |
File usage
The following pages on the English Wikipedia use this file (pages on other projects are not listed):
Metadata
This file contains additional information, probably added from the digital camera or scanner used to create or digitize it.
If the file has been modified from its original state, some details may not fully reflect the modified file.
Software used | |
---|---|
Horizontal resolution | 39.37 dpc |
Vertical resolution | 39.37 dpc |
Retrieved from "https://en.wikipedia.org/wiki/File:Loop_erased_random_walk_in_2D.png"