@@ -3,25 +3,73 @@ import { Container } from "react-bootstrap";
33import  AlgorithmForm  from  "./AlgorithmForm" ; 
44import  AlgorithmInfo  from  "./AlgorithmInfo" ; 
55import  type  {  GraphProps  }  from  "../types/info" ; 
6- 
7- const  generateRandomArray  =  ( length  =  50 ,  min  =  20 ,  max  =  200 ) : number [ ]  => 
8-   Array . from ( 
9-     {  length } , 
10-     ( )  =>  Math . floor ( Math . random ( )  *  ( max  -  min  +  1 ) )  +  min 
11-   ) ; 
6+ import  {  conflict  }  from  "../services/graphAlgorithms/helper" ; 
7+ import  type  {  Item  }  from  "../types/item" ; 
8+ import  "../css/Graphs.css" ; 
129
1310const  TraversalVisualizer : React . FC < GraphProps >  =  ( {  graphsInfo } )  =>  { 
14-   const  [ array ,  setArray ]  =  useState < number [ ] > ( [ ] ) ; 
15-   const  [ algorithm ,  setAlgorithm ]  =  useState < string > ( "" ) ; 
11+   const  [ matrix ,  setMatrix ]  =  useState < any [ ] [ ] > ( [ ] ) ; 
12+   const  [ start ,  setStart ]  =  useState ( {  row : 0 ,  col : 0  } ) ; 
13+   const  [ objectives ,  setObjectives ]  =  useState < Item [ ] > ( [ ] ) ; 
14+   const  [ obstacles ,  setObstacles ]  =  useState < Item [ ] > ( [ ] ) ; 
15+   const  [ weights ,  setWeights ]  =  useState < Item [ ] > ( [ ] ) ; 
16+   const  [ algorithm ,  setAlgorithm ]  =  useState ( "" ) ; 
1617  const  [ isSorting ,  setIsSorting ]  =  useState ( false ) ; 
1718
19+   const  generateMatrix  =  ( )  =>  { 
20+     const  n  =  30 ;  // nxn array 
21+     const  newMatrix  =  Array . from ( {  length : n  } ,  ( )  =>  Array ( 50 ) . fill ( " " ) ) ;  // generate array 
22+     const  objectivesArray  =  [ ] ;  // array to hold bojectives 
23+     const  obstaclesArray  =  [ ] ;  // array to hold obstacles 
24+     const  weightsArray  =  [ ] ;  // array to hold obstacles 
25+     const  numObjectives  =  1 ;  // number of objectives 
26+     const  numObstacles  =  100 ;  // number of obstacles 
27+     const  numWeights  =  50 ;  // number of weights 
28+     let  placed  =  0 ; 
29+     // create and set the starting point 
30+     let  x  =  Math . floor ( Math . random ( )  *  newMatrix . length ) ; 
31+     let  y  =  Math . floor ( Math . random ( )  *  newMatrix [ 0 ] . length ) ; 
32+ 
33+     setStart ( {  row : x ,  col : y  } ) ; 
34+     newMatrix [ x ] [ y ]  =  "s" ; 
35+ 
36+     while  ( placed  <  numObjectives  +  numObstacles  +  numWeights )  { 
37+       // generate random x and y cord 
38+       x  =  Math . floor ( Math . random ( )  *  newMatrix . length ) ; 
39+       y  =  Math . floor ( Math . random ( )  *  newMatrix [ 0 ] . length ) ; 
40+       if  ( ! conflict ( x ,  y ,  newMatrix ) )  { 
41+         // if not conflict 
42+         if  ( placed  ===  0 )  { 
43+           objectivesArray . push ( {  row : x ,  col : y  } ) ;  // add to objective 
44+           newMatrix [ x ] [ y ]  =  "o" ; 
45+         }  else  if  ( 
46+           placed  >=  numObjectives  && 
47+           placed  <=  numObjectives  +  numObstacles 
48+         )  { 
49+           obstaclesArray . push ( {  row : x ,  col : y  } ) ;  // add to obstacles 
50+           newMatrix [ x ] [ y ]  =  "w" ; 
51+         }  else  { 
52+           weightsArray . push ( {  row : x ,  col : y  } ) ;  // add to weight 
53+           newMatrix [ x ] [ y ]  =  "e" ; 
54+         } 
55+         placed  +=  1 ; 
56+       } 
57+     } 
58+ 
59+     setObjectives ( objectivesArray ) ; 
60+     setObstacles ( obstaclesArray ) ; 
61+     setWeights ( weightsArray ) ; 
62+     // setMatrix(newMatrix); 
63+     return  newMatrix ; 
64+   } ; 
65+ 
1866  useEffect ( ( )  =>  { 
19-     setArray ( generateRandomArray ( ) ) ; 
67+     setMatrix ( generateMatrix ( ) ) ; 
2068  } ,  [ ] ) ; 
2169
22-   const  resetArray  =  ( )  =>  { 
70+   const  resetMatrix  =  ( )  =>  { 
2371    if  ( ! isSorting )  { 
24-       setArray ( generateRandomArray ( ) ) ; 
72+       setMatrix ( generateMatrix ( ) ) ; 
2573    } 
2674  } ; 
2775
@@ -34,39 +82,82 @@ const TraversalVisualizer: React.FC<GraphProps> = ({ graphsInfo }) => {
3482    if  ( ! algorithm  ||  isSorting )  return ; 
3583    setIsSorting ( true ) ; 
3684
37-     await  fakeSort ( array ,  setArray ,  algorithm ) ; 
85+     //  await fakeSort(array, setArray, algorithm);
3886
3987    setIsSorting ( false ) ; 
4088  } ; 
4189
4290  return  ( 
43-     < Container  className = "d-flex flex-column justify-content-center align-items-center min-vh-100" > 
44-       < div  className = "array-container mb-4" > 
45-         { array . map ( ( value ,  idx )  =>  ( 
46-           < div 
47-             key = { idx } 
48-             className = "array-bar" 
49-             style = { {  height : `${ value }  px`  } } 
50-           > </ div > 
91+     < div  className = "GraphTraversalVisualiser container d-flex flex-column align-items-center py-4" > 
92+       < div  className = "matrix-container d-flex flex-column mb-4" > 
93+         { matrix . map ( ( row ,  rowIndex )  =>  ( 
94+           < div  key = { rowIndex }  className = { "matrix-row matrix-row-"  +  rowIndex } > 
95+             { row . map ( ( cell ,  colIndex )  =>  ( 
96+               < div 
97+                 key = { colIndex } 
98+                 className = { `matrix-cell 
99+                             ${  
100+                               start . row  ===  rowIndex  &&  start . col  ===  colIndex  
101+                                 ? "start"  
102+                                 : ""  
103+                             }  
104+                             ${  
105+                               objectives . some (  
106+                                 ( obj )  =>  
107+                                   obj . row  ===  rowIndex  &&  obj . col  ===  colIndex  
108+                               )  
109+                                 ? "objective"  
110+                                 : ""  
111+                             }  
112+                             ${  
113+                               obstacles . some (  
114+                                 ( obs )  =>  
115+                                   obs . row  ===  rowIndex  &&  obs . col  ===  colIndex  
116+                               )  
117+                                 ? "obstacle"  
118+                                 : ""  
119+                             }   
120+                             ${  
121+                               weights . some (  
122+                                 ( weight )  =>  
123+                                   weight . row  ===  rowIndex  &&  
124+                                   weight . col  ===  colIndex  
125+                               )  
126+                                 ? "weight"  
127+                                 : ""  
128+                             }  
129+                             col-index-${ colIndex }  ` } 
130+               > 
131+                 { cell } 
132+               </ div > 
133+             ) ) } 
134+           </ div > 
51135        ) ) } 
52136      </ div > 
53- 
54-       < div  style = { {  maxWidth : "400px" ,  width : "100%"  } } > 
55-         < AlgorithmForm 
56-           value = { algorithm } 
57-           options = { [ "Merge" ,  "Bubble" ,  "Selection" ,  "Insertion" ] } 
58-           onChange = { handleChange } 
59-           onSubmit = { handleSubmit } 
60-           onReset = { resetArray } 
61-           disabled = { isSorting } 
62-         /> 
137+       < div  className = "legend mb-4 text-center" > 
138+         < ul > 
139+           < li  className = "start" > Start</ li > 
140+           < li  className = "objective" > Objective</ li > 
141+           < li  className = "path" > Path</ li > 
142+           < li  className = "obstacle" > Obstacle</ li > 
143+           < li  className = "expanded" > Expanded Nodes</ li > 
144+           < li  className = "weight" > Weighted (ignored for bfs and dfs)</ li > 
145+         </ ul > 
63146      </ div > 
147+       < AlgorithmForm 
148+         value = { algorithm } 
149+         options = { [ "Astar" ,  "BFS" ,  "DFS" ,  "UCS" ] } 
150+         onChange = { handleChange } 
151+         onSubmit = { handleSubmit } 
152+         onReset = { resetMatrix } 
153+         disabled = { isSorting } 
154+       /> 
64155
65156      < Container  className = "mt-5" > 
66157        < h2  className = "text-center mb-4" > About Sorting Algorithms</ h2 > 
67158        < AlgorithmInfo  items = { graphsInfo }  /> 
68159      </ Container > 
69-     </ Container > 
160+     </ div > 
70161  ) ; 
71162} ; 
72163
0 commit comments