Advertisement
tomuwhu

gráf mélységi és szélességi bejárása

Oct 3rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <div id=id>
  3.     <button @click="mb(0)">Bejár</button>
  4.     <div v-for="(e,i) in mj"> {{i}} : {{e}}
  5.     </div>    
  6.     </div>
  7. </template>
  8.  
  9. <script>
  10. let g=[
  11.     [1,2],
  12.     [4,5],
  13.     [3],
  14.     [5],
  15.     [2],
  16.     []
  17. ], i=1, p,
  18. asz = {
  19.     adat: [],
  20.     betesz(p) {
  21.         this.adat.push(p)
  22.     },
  23.     kivesz() {
  24.         //return this.adat.shift()  //szélességi
  25.         return this.adat.pop()   //mélységi
  26.     }
  27. }
  28.  
  29. export default {
  30.     data: () => ({
  31.         mj: Array(7).fill(0)
  32.     }),
  33.     methods: {
  34.         mb(p) {
  35.             this.$set(this.mj,p,i++) ;
  36.             asz.betesz(p) ;
  37.             while (asz.adat.length) {
  38.                 p = asz.kivesz() ;
  39.                 g[p].forEach( v => {
  40.                     if (!this.mj[v]) {
  41.                         this.$set(this.mj,v,i++) ;
  42.                         asz.betesz(v) ;
  43.                     }
  44.                 })
  45.             }
  46.         }
  47.     }
  48. }
  49. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement