File:Loop erased random walk in 2D.png

Page contents not supported in other languages.
This is a file from the Wikimedia Commons
From Wikipedia, the free encyclopedia

Original file(1,600 × 1,600 pixels, file size: 176 KB, MIME type: image/png)

Summary

Description
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:
w:en:Creative Commons
attribution share alike
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.

Captions

Add a one-line explanation of what this file represents

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/TimeThumbnailDimensionsUserComment
current05:07, 25 November 2023Thumbnail for version as of 05:07, 25 November 20231,600 × 1,600 (176 KB)Cosmia NebulaUploaded while editing "Loop-erased random walk" on en.wikipedia.org
The following pages on the English Wikipedia use this file (pages on other projects are not listed):

Metadata