Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Siccity · Mar 16, 2014 at 10:01 AM · c#arraylistpathfindingnodes

Pathfinding through pairs of connections

Okay this is a bit of a complicated problem. I have a List called pipeData. The list contains sets of two ints, call these ints points. The two points form a connection, e.g. (1,3) (4,2) (2,3) (4,3). Now, what I need is to find a way to check if, say, point 4 is connected to point 1. The function then needs to search through the (4,3), then (1,3) and return true. If no connection is found then return false. The number of connections will be somewhere between 10-40, but is theoretically uncapped because the user creates these points in an editor. The number of connections between the points is also uncapped, and the function will run on button press, not on update.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 
 public class EditorChassis : MonoBehaviour {
 
     List<int[]> pipeData = new List<int[]>();
 
     void Start() {
         //Create a random connection list to test against
         string debug = "Connections created:";
         for(int i = 0;i<20;i++) {
             int pointA = Random.Range(0,20);
             int pointB = Random.Range(0,20);
             temp_pipeData.Add = new int[] {pointA,pointB}
             debug += " ("+pointA+","+pointB+")";
         }
         print(debug);
         print(Connected(0,20));
     }
 
     bool Connected(int start,int end) {
         //return true if connected, false if not
     }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Mar 17, 2014 at 01:33 PM 0
Share

If you want to do some research on your own (you should), you should have a look at graph and possibly Graph theory. Your problem is called the shortest path problem. There are several algorithms that solve this efficiently, look up Dijkstra's Algorithm or A*.

When implementing your algorithm of choice, remember that the vertices might be in different graphs, and thus a solution might not be found.

avatar image fafase · Mar 17, 2014 at 01:35 PM 0
Share

He is going with nodes and tree (at least it looks like) DFS/BFS are more appropriate since he is not looking for the shortest path but just if they are related.

avatar image Jamora · Mar 17, 2014 at 01:42 PM 0
Share

@fafase True, but the easiest way of discovering this is by finding the shortest path. The algorithm you described below your answer is actually Dijkstra's Algorithm in an undirected&unweighted graph. (Yes, it's just a BFS.)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Siccity · Mar 17, 2014 at 05:39 PM

This is exactly what I was looking for, I'll throw it on here for future reference. Thank you for your help

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
  
 public class EditorChassis : MonoBehaviour {
  
     List<int[]> pipeData = new List<int[]>();
     List<int> visited = new List<int>();
 
     void Start() {
        //Create a random connection list to test against
        string debug = "Connections created:";
        for(int i = 0;i<20;i++) {
          int pointA = Random.Range(0,20);
          int pointB = Random.Range(0,20);
          temp_pipeData.Add = new int[] {pointA,pointB}
          debug += " ("+pointA+","+pointB+")";
        }
        print(debug);
        print(Connected(0,20));
     }
  
     bool Connected(int start,int end) {
        visited.Clear();
        return Connection(start,end);
     }
 
     bool Connection(int pointA,int pointB){
         if (pointA == pointB) return true;
         for(int i = 0;i<pipeData.Count;i++) {
             if (!visited.Contains(i)) {
                 if (pipeData[i][0] == pointA) {
                     visited.Add(i);
                     if (Connection(pipeData[i][1],pointB) == true) return true;
                 }
                 else if (pipeData[i][1] == pointA) {
                     visited.Add(i);
                     if (Connection(pipeData[i][0],pointB) == true) return true;
                 }
             }
         }
         return false;
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Mar 18, 2014 at 10:03 AM 0
Share

One drawback of your implementation is the limited size it affords. See You are using recursive function, which means your Connection method calls Connection method within it and this may happens many times. This works for relative small size tree, try this on a Google server system and you will soon be facing a stack overflow exception. That is because each method call is piling new variable on the stack but the previous ones remains until we reach the bottom of the tree.

You may want to consider queuing and while loop ins$$anonymous$$d of for loop and call to Connection method over and over again.

avatar image Siccity · Mar 18, 2014 at 12:52 PM 0
Share

I've never used queuing before and I don't know how to use it, could you give me an example of what you mean?

avatar image fafase · Mar 19, 2014 at 05:02 AM 0
Share

This is for queue : http://en.wikipedia.org/wiki/FIFO and its buddy the stack :http://en.wikipedia.org/wiki/LIFO_(computing)

avatar image
0

Answer by fafase · Mar 16, 2014 at 10:05 AM

You could create a Node class or stucture where you store the info you want.

 public class Node{
     private Vector2 position;
     public Vector2 Position{get{return position;}}
     public List<Node> connections = new List<Node>();
     
     public Node(Vector2 position){
         this.position = position;
     }
     // You don't need the method, it is just to show which it is
     public bool Contains(Node node){
          return connection.Contains(node);
     }
 }

Note that the members should maybe be private.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Siccity · Mar 16, 2014 at 01:29 PM 0
Share

The user is allowed to add and remove these connections freely, and there is no hierarchy of the nodes. The project is large and moving from List to Node() is not possible. Or maybe I misunderstood your answer? If so, please continue

avatar image fafase · Mar 16, 2014 at 06:27 PM 0
Share

Edited the answers with List

avatar image Siccity · Mar 17, 2014 at 12:35 PM 0
Share

I don't think I explained myself correctly. I specified points as (1,5) (5,2) (4,3) etc. These are NOT Vector2's, they are pairs - bridges. By (1,5) it means that this pair connects point 1 and 5 together, creating a bridge between the two. To test between two points you'll need first a pair that contains the starting point, then, using the second point in the pair, find another entry, to expand the 'connection' Eg. "'are 1 and 3 connected?' (1,5)-(5,7)-(2,7)-(2,3) therefore 'true'". Again, the pair is not a Vector2 and does not contain x and y values, it's a bridge between points.

avatar image fafase · Mar 17, 2014 at 01:23 PM 0
Share

Ok, so what you need is a tree of nodes and use DFS or BFS algorithm.

You can remove the position from the node but you should keep the list and add a parent node and a already visited bool.

Let's consider you have a tree that looks like this:

             1
           $$anonymous$$__
          |     |
          5     6
        $$anonymous$$__
       |     |
       8     7
           $$anonymous$$__
          |     |
          4     3

Then you take the starting node 1, and you enqueue in a queue the list of node connected to it (5,6). In the same time, you mark those with the starting node as parent. Then dequeue (you get 5), has it been visited, nope, mark as visited, is it the end node, nope, enqueue all linked nodes with 5 as parent(6,8,7). Dequeue, you get 6, has it been visited. nope, mark as visited, is it the end node, nope, nothing to enqueue. Dequeue, you have 8 same again, marked as visited, not the end, add to the queue with 8 as parent(nothing to queue). Take 7, not the end (shorting), 4 and 3 are added. Now we are going to get to 3 and it is the end, happens to be it was the last one. So we just need to go up the parents and store them in a list: 3 has 7, 7 has 5, 5 has 1

3->7->5->1 revert that list and make it an array and that is it.

avatar image Siccity · Mar 17, 2014 at 05:28 PM 0
Share

So I spoke to a $$anonymous$$cher of $$anonymous$$e, and he explained exactly what you just said. I'll post the solution in a new answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom pathfinding and node-values 1 Answer

A node in a childnode? 1 Answer

How to send/copy array from dictionary to class and further to list of class 0 Answers

Remove object from List after gameObject was destroyed in between Scene loads 1 Answer

Dynamic Enemy Transform List 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges