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 DanielJF · Mar 17, 2014 at 10:41 AM · waypointdepthalgorithm

DFS & BFS help needed

Hello everyone,

I have been asking before about a Tree structure waypoint system and the awnsers helped me alot. Now i run into the next point. I made a bfs and dfs function to test them both out and they sort of work the problem i have is that i also have waypoints that link with each other so there are multiple options to run. Anyone a good idea how i could fix this best.

Thx already for the help this is my code:

 [Serializable]
 public class TreeNode
 {
     public string value;
     public List<TreeNode> children;
     public TreeNode( string s )
     {
         value = s;
         children = new List<TreeNode>();
     }
 
     public List<TreeNode> FindPath(TreeNode target)
     {
         List<TreeNode> path = new List<TreeNode>();
         path.Add(this);
         if (target == this)
         {
             return path;
         }
 
         foreach (TreeNode tn in children)
         {
             List<TreeNode> childPath = tn.FindPath(target);
             if (childPath != null)
             {
                 path.AddRange(childPath);
                 return path;
             }
         }
         return null;
     }
 }
 
 public class DFSTest : MonoBehaviour
 {
     public GameObject startPoint;
     private GameObject gameObjectHolder;
     private Queue<TreeNode> tnQueue = new Queue<TreeNode>(); 
     public List<string> testList = new List<string>(); 
 
     public List<TreeNode> root = new List<TreeNode>();
     private TreeNode target;
     private List<string> treeNodeNames = new List<string>();
 
     private void OnMouseDown()
     {
         DFSearch();
     }
 
     void BFSearch()
     {
         tnQueue = new Queue<TreeNode>();
         treeNodeNames = new List<string>();
         tnQueue.Enqueue(new TreeNode(startPoint.GetComponent<InfoHolder>().Infos.str));
         treeNodeNames.Add(tnQueue.Peek().value);
         LoopFunctionBF(startPoint);
     }
 
     void LoopFunctionBF(GameObject holderObject)
     {
         while (tnQueue.Count>0)
         {
             TreeNode tn = tnQueue.Dequeue();
             testList.Add(tn.value);
             if (tn.value == "g2")
             {
                 print("found it");
                 return;
             }
 
             foreach (GameObject treeNode in holderObject.GetComponent<InfoHolder>().Infos.NextInLineList)
             {
                 var holder = treeNode.GetComponent<InfoHolder>().Infos.str;
                 if (!treeNodeNames.Contains(holder))
                 {
                     tnQueue.Enqueue(new TreeNode(holder));
                     treeNodeNames.Add(holder);
                     print(treeNode.GetComponent<InfoHolder>().Infos.str);
                     LoopFunctionBF(treeNode);
                 }
             }
         }
     }
 
     void DFSearch()
     {
         root = new List<TreeNode>();
         treeNodeNames = new List<string>();
 
         root.Add(new TreeNode(startPoint.GetComponent<InfoHolder>().Infos.str));
         treeNodeNames.Add(root[0].value);
         LoopFunctionDF(startPoint, root[0].children);
         if (root[0].children.Count > 0 && root[0].children[0].children.Count != 0)
         {
             target = root[0].children[1].children[0].children[0].children[0].children[0].children[0].children[0];
         }
 
         else if (root[0].children.Count > 0)
         {
             target = root[0].children[0];
         }
         else
         {
             return;
         }
 
         List<TreeNode> path = root[0].FindPath(target);
         string stringPath = "";
 
         foreach (TreeNode treeNode in path)
         {
             if (treeNode != root[0])
             {
                 stringPath += "->";
             }
             stringPath += treeNode.value;
         }
         Debug.Log(stringPath);
     }
 
     private void LoopFunctionDF(GameObject checkObject, List<TreeNode> tnList)
     {
         foreach (GameObject o in checkObject.GetComponent<InfoHolder>().Infos.NextInLineList)
         {
             string oHolder = o.GetComponent<InfoHolder>().Infos.str;
 
             bool containsObject = treeNodeNames.Contains(oHolder);
 
             if (oHolder == "g2")
             {
                 treeNodeNames.Add(oHolder);
                 tnList.Add(new TreeNode(oHolder));
                 print("found target");
                 return;
             }
 
             if (!containsObject)
             {
                 print(oHolder);
                 treeNodeNames.Add( oHolder );
                 tnList.Add(new TreeNode(oHolder));
                 LoopFunctionDF(o, tnList.Last().children);
             }
         }
     }
 }

It's atm testing code so lil bit messy but it should be readable.

Comment
Add comment
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

1 Reply

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

Answer by DanielJF · Mar 17, 2014 at 04:02 PM

Fixed it i forgot some connections in the inspector.

Comment
Add comment · 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

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

20 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

Related Questions

Waypoint system suggestions 1 Answer

Depth Sorting of Billboard Particles, how can I do it? 3 Answers

Error with the bfs algorithm That I can not understand 1 Answer

Render depth to RenderTexture and calculate world position 1 Answer

Depth with Unity2D 2 Answers


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