เดินบน Hypercube


9

ฉันเพิ่งอ่านทฤษฎีกราฟโดยเฉพาะhypercubesและคิดเกี่ยวกับวิธีที่น่าสนใจในการสร้างเส้นทางบนพวกเขา นี่คือสิ่งที่ฉันมาด้วย

ดังที่คุณอาจทราบแล้วว่าคุณสามารถสร้างไฮเปอร์คิวบ์ n- มิติได้โดยนำ n-tuples ทั้งหมดที่ประกอบไปด้วย1และ0เป็นจุดยอดและเชื่อมต่อมันถ้ามันต่างกันในหลักเดียว หากคุณตีความเลขฐานสองเหล่านี้เป็นตัวเลขจำนวนเต็มคุณจะได้กราฟที่มีจุดยอดที่เป็นตัวเลข ตัวอย่างเช่นn=3:

ป้อนคำอธิบายรูปภาพที่นี่

สมมติว่าคุณต้องการที่จะใช้เวลาเดินบน hypercube 0นี้และเริ่มต้นที่จุดสุดยอด ตอนนี้คุณจะกำหนดจุดสุดยอดที่คุณต้องการเยี่ยมชมต่อไปได้อย่างไร กฎที่ฉันใช้คือนำจำนวนaจุดยอดที่คุณอยู่พลิกmod(a,n)บิตของมัน(การทำดัชนีแบบ zero-based) แล้วไปที่จุดสุดยอดที่เกิดขึ้น อย่างเป็นทางการกฎนี้สามารถกำหนดซ้ำเป็น

a[m+1] = xor(a[m], 2^mod(a[m],n)).

โดยทำตามกฎนี้คุณจะยังคงอยู่บนลูกบาศก์และเดินทางไปตามขอบ เส้นทางที่ได้จะมีลักษณะเช่นนี้

ป้อนคำอธิบายรูปภาพที่นี่

อย่างที่คุณเห็นคุณจะเดินเป็นวงกลม! อันที่จริงในทุกมิติและสำหรับทุกจุดเริ่มต้นเส้นทางของคุณจะจบลงด้วยการวนซ้ำ ตัวอย่างเช่นn=14และa[0]=0ดูเหมือนว่านี้

ป้อนคำอธิบายรูปภาพที่นี่

สำหรับนักวางแผนตัวยงความยาวของเส้นทางที่วางแผนไว้เป็นข้อมูลสำคัญ ดังนั้นงานของคุณคือการเขียนฟังก์ชั่นหรือโปรแกรมที่ใช้มิติ hypercube nจุดยอดเริ่มต้นa[0]เป็นอินพุตและเอาต์พุตจำนวนจุดยอดในลูปผลลัพธ์

กรณีทดสอบ

n   a[0]   Output
-----------------
3   0      6
14  0      50
5   6      8
17  3      346

กฎระเบียบ

  • ช่องโหว่มาตรฐานเป็นสิ่งต้องห้าม
  • เอาต์พุต / อินพุตอาจอยู่ในรูปแบบที่เหมาะสม
  • คุณอาจถือว่าa[0]เป็นจุดสุดยอดที่ถูกต้อง

เกณฑ์การให้คะแนน

รหัสที่สั้นที่สุดเป็นไบต์ชนะ

หากคุณมีข้อมูลเพิ่มเติมเกี่ยวกับหัวข้อนี้ฉันยินดีที่จะได้ยิน!


ตามกฎa[m+1] = xor(a[m], 2^mod(a[m],n))แล้วมันไม่เกี่ยวข้องถ้าจุดยอดเป็นของ hypercube ใช่ไหม?
Luis Mendo

ขวา. หากa[m]อยู่ใน hypercube a[m+1]ก็จะเป็นเช่นกัน และในขณะที่คุณสามารถถือว่าa[0]เป็นจุดสุดยอดที่ถูกต้องคุณแทบไม่จำเป็นต้องใส่ใจกับสิ่งใด ๆ ของ hypercube และเพียงแค่ทำตามกฎ
murphy

คำตอบ:


4

เยลลี่ขนาด 9 ไบต์

%⁴2*^µÐḶL

รับอาร์กิวเมนต์บรรทัดรับคำสั่งสองรายการ

%⁴2*^µÐḶL        A monadic link. Inputs: a_0. b also taken from command line.
%⁴2*^              Variadic link. Input: a
%⁴                   a modulo b. ⁴ is second input, b.
  2*                 Get 2 to that power
    ^                and bitwise xor with a.
     µ             Start a new, monadic link (input: a_0)
      ÐḶ             All elements of the cycle created when the preceding link
                     is applied repeatedly, starting with a_0.
        L            Length.

ลองมันนี่


2

Haskell, 124

import Data.Bits
(y:z:w)%(x:s)|x==y||x==z=[i|(i,r)<-zip[1..]s,r==x]!!0|0<1=w%s
g n=(tail>>=(%)).iterate(\a->xor a$2^mod a n)

นี่เป็นการค้นหาวงกลมโดยอัลกอริธึมสองพอยน์เตอร์กำลังไปรอบ ๆ ในความเร็วที่แตกต่างกันและใช้ / ใช้อย่างไม่เหมาะสมในรายการของ Haskell (ตัวอย่างเช่นทั้งสองพอยน์เตอร์เป็นรายการจริง)

gเป็นฟังก์ชั่นที่คำนวณคำตอบ ให้nแล้วa[0]มันจะส่งคืนหมายเลขให้คุณ (โปรดทราบว่าnควรกำหนดให้เป็นประเภทIntเพื่อหลีกเลี่ยงความกำกวมประเภท)


1

JavaScript (ES6), 69 ไบต์

(n,a)=>{g=m=>m^1<<m%n;for(c=1,b=a;(b=g(g(b)))!=(a=g(a));)c++;return c}

ส่งคืน 18812 สำหรับ (23, 10)


1

MATL , 38 37 28 ไบต์

xi`vt0)2y1G\^Z~yywP=fn~]2M1$

ใช้ได้กับภาษาปัจจุบัน (15.0.0)

ลองออนไลน์ !

คำอธิบาย

x       % take first input: n. Delete (gets copied into clipboard G)
i       % take second input: initial value of a
`       % do...while loop
  v     %   concatenate all stack contents vertically
  t0)   %   duplicate. Get last element of that array: current a
  2     %   push 2
  y     %   duplicate second-top element in stack: current a
  1G    %   push first input (n)
  \     %   a modulo n
  ^     %   2 raised to that
  Z~    %   xor of that with current a
  yy    %   duplicate top two elements in stack: array of old a's and new a
  w     %   swap: move array of old a's to top
  P     %   reverse that array. So first entry is most recent a (before current)
  =f    %   indices of old values that equal current value. There may be 0 or 1
  n~    %   is it empty?
]       % if so, continue with a new iteration
2M      % push array of indices. It contains exactly 1 index
1$      % set 1 input for implicit display function, so it only displays the index

@lirtosiast จริง! ขอบคุณ แก้ไขแล้ว
Luis Mendo

1

Pyth, 22 17 ไบต์

Lx^2%bQbl.uyNuyGE

คำอธิบาย:

Lx^2%bQbl.uyNuyGE     Implicit: Q=first line n. E=second line a[0].
Lx^2%bQb              y = lambda b: do one iteration
                      Then
             uyGE     Apply y until a previous result is found.
                      This makes sure we're in the cycle.
         .uyN         Then apply y again until a previous result is found.
                      Keep all intermediate values but not the repeat.
        l             Get the length; i.e. the length of the cycle.

ลองมันนี่

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.