Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions Advanced_Program_BFSMaze/BFS.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type Pair struct {
Row, Col int
}

func concurrentCheckNeighbor(cell Pair, nextFrontier *[]Pair, visited *[][]Pair, mu_visited *sync.Mutex, mu_frontier *sync.Mutex, parentCell Pair, numRows int, numCols int, success *int32) {
if cell.Row == numRows && cell.Col == numCols {
func concurrentCheckNeighbor(cell Pair, nextFrontier *[]Pair, visited *[][]Pair, mu_visited *sync.Mutex, mu_frontier *sync.Mutex, parentCell Pair, targetRow int, targetCol int, success *int32) {
if cell.Row == targetRow && cell.Col == targetCol {
atomic.AddInt32(success, 1)
}
mu_visited.Lock()
Expand All @@ -32,19 +32,19 @@ func concurrentCheckNeighbor(cell Pair, nextFrontier *[]Pair, visited *[][]Pair,

}

func concurrentOperation(maze [][]*Cell, cell Pair, nextFrontier *[]Pair, visited *[][]Pair, wg *sync.WaitGroup, mu_visited *sync.Mutex, mu_frontier *sync.Mutex, numRows int, numCols int, success *int32) {
func concurrentOperation(maze [][]*Cell, cell Pair, nextFrontier *[]Pair, visited *[][]Pair, wg *sync.WaitGroup, mu_visited *sync.Mutex, mu_frontier *sync.Mutex, targetRow int, targetCol int, success *int32) {
defer wg.Done()
if !maze[cell.Row][cell.Col].Top {
concurrentCheckNeighbor(Pair{Row: cell.Row - 1, Col: cell.Col}, nextFrontier, visited, mu_visited, mu_frontier, cell, numRows, numCols, success)
concurrentCheckNeighbor(Pair{Row: cell.Row - 1, Col: cell.Col}, nextFrontier, visited, mu_visited, mu_frontier, cell, targetRow, targetCol, success)
}
if !maze[cell.Row][cell.Col].Right {
concurrentCheckNeighbor(Pair{Row: cell.Row, Col: cell.Col + 1}, nextFrontier, visited, mu_visited, mu_frontier, cell, numRows, numCols, success)
concurrentCheckNeighbor(Pair{Row: cell.Row, Col: cell.Col + 1}, nextFrontier, visited, mu_visited, mu_frontier, cell, targetRow, targetCol, success)
}
if !maze[cell.Row][cell.Col].Bottom {
concurrentCheckNeighbor(Pair{Row: cell.Row + 1, Col: cell.Col}, nextFrontier, visited, mu_visited, mu_frontier, cell, numRows, numCols, success)
concurrentCheckNeighbor(Pair{Row: cell.Row + 1, Col: cell.Col}, nextFrontier, visited, mu_visited, mu_frontier, cell, targetRow, targetCol, success)
}
if !maze[cell.Row][cell.Col].Left {
concurrentCheckNeighbor(Pair{Row: cell.Row, Col: cell.Col - 1}, nextFrontier, visited, mu_visited, mu_frontier, cell, numRows, numCols, success)
concurrentCheckNeighbor(Pair{Row: cell.Row, Col: cell.Col - 1}, nextFrontier, visited, mu_visited, mu_frontier, cell, targetRow, targetCol, success)
}
}

Expand Down Expand Up @@ -94,30 +94,30 @@ func ParallelBFS(maze [][]*Cell, numRows int, numCols int) ([]Pair, [][]Pair) {
return reconstructPath(visited, Pair{Row: 0, Col: 0}, Pair{Row: numRows - 1, Col: numCols - 1}), visited
}

func checkEnd(cell Pair, numRows int, numCols int, success *int) {
if cell.Row == numRows && cell.Col == numCols {
func checkEnd(cell Pair, targetRow int, targetCol int, success *int) {
if cell.Row == targetRow && cell.Col == targetCol {
*success = 1
}
}

func SequentialFindNeighbors(maze [][]*Cell, cell Pair, nextFrontier *[]Pair, visited *[][]Pair, numRows int, numCols int, success *int) {
func SequentialFindNeighbors(maze [][]*Cell, cell Pair, nextFrontier *[]Pair, visited *[][]Pair, targetRow int, targetCol int, success *int) {
if !maze[cell.Row][cell.Col].Top && (*visited)[cell.Row-1][cell.Col] == (Pair{-1, -1}) {
checkEnd(Pair{Row: cell.Row - 1, Col: cell.Col}, numRows, numCols, success)
checkEnd(Pair{Row: cell.Row - 1, Col: cell.Col}, targetRow, targetCol, success)
(*visited)[cell.Row-1][cell.Col] = Pair{Row: cell.Row, Col: cell.Col}
*nextFrontier = append(*nextFrontier, Pair{Row: cell.Row - 1, Col: cell.Col})
}
if !maze[cell.Row][cell.Col].Right && (*visited)[cell.Row][cell.Col+1] == (Pair{-1, -1}) {
checkEnd(Pair{Row: cell.Row, Col: cell.Col + 1}, numRows, numCols, success)
checkEnd(Pair{Row: cell.Row, Col: cell.Col + 1}, targetRow, targetCol, success)
(*visited)[cell.Row][cell.Col+1] = Pair{Row: cell.Row, Col: cell.Col}
*nextFrontier = append(*nextFrontier, Pair{Row: cell.Row, Col: cell.Col + 1})
}
if !maze[cell.Row][cell.Col].Bottom && (*visited)[cell.Row+1][cell.Col] == (Pair{-1, -1}) {
checkEnd(Pair{Row: cell.Row + 1, Col: cell.Col}, numRows, numCols, success)
checkEnd(Pair{Row: cell.Row + 1, Col: cell.Col}, targetRow, targetCol, success)
(*visited)[cell.Row+1][cell.Col] = Pair{Row: cell.Row, Col: cell.Col}
*nextFrontier = append(*nextFrontier, Pair{Row: cell.Row + 1, Col: cell.Col})
}
if !maze[cell.Row][cell.Col].Left && (*visited)[cell.Row][cell.Col-1] == (Pair{-1, -1}) {
checkEnd(Pair{Row: cell.Row, Col: cell.Col - 1}, numRows, numCols, success)
checkEnd(Pair{Row: cell.Row, Col: cell.Col - 1}, targetRow, targetCol, success)
(*visited)[cell.Row][cell.Col-1] = Pair{Row: cell.Row, Col: cell.Col}
*nextFrontier = append(*nextFrontier, Pair{Row: cell.Row, Col: cell.Col - 1})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Testing BFS for IT327 Advanced program
// Author: Brady McCue
// Date: 12/7/2025

package main

import (
"fmt"
"time"
)

func printMaze(m *Maze) {
func checkSolutionPath(path []Pair, point Pair) bool {
for i := range path {
if path[i] == point {
return true
}
}
return false
}
func printMaze(m *Maze, path []Pair) {
for r := 0; r < m.Height; r++ {
for c := 0; c < m.Width; c++ {
if m.Cells[r][c].Top {
Expand All @@ -25,7 +34,12 @@ func printMaze(m *Maze) {
} else {
fmt.Print(" ")
}
fmt.Print(" ")
if (checkSolutionPath(path, Pair{Row: r, Col: c})) {
fmt.Print(" X ")

} else {
fmt.Print(" ")
}
}

if m.Cells[r][m.Width-1].Right {
Expand All @@ -44,39 +58,15 @@ func printMaze(m *Maze) {
fmt.Println()
}

/* bad don't use
func printPath(visitedArray [][]Pair) {

goal := visitedArray[len(visitedArray)-1][len(visitedArray[0])-1]
start := visitedArray[0][0]
current := goal

goalColumn := len(visitedArray[0]) - 1
goalRow := len(visitedArray) - 1

i := goalRow
j := goalColumn
fmt.Println("Path from Goal to start found: ")
//backtracking from goal to start
for current != start {
fmt.Printf("Cell (%d, %d) came from (%d, %d)\n", i, j, visitedArray[i][j].Row, visitedArray[i][j].Col)
i = visitedArray[i][j].Row
j = visitedArray[i][j].Col
current = visitedArray[i][j]
}

}
*/

func main() {
Maze := CreateMaze(10, 10)
Maze := CreateMaze(100, 100)
//print maze, taken from Maze.cpp reference file
printMaze(Maze)
printMaze(Maze, nil)

startBFSTime := time.Now()
fmt.Println("Starting Parallel BFS...")
fmt.Println("\nStarting Parallel BFS...")

path, _ := ParallelBFS(Maze.Cells, 10, 10)
path, _ := ParallelBFS(Maze.Cells, 100, 100)
//Maze.DS.PrintArrayValues()

elapsedBFSTime := time.Since(startBFSTime)
Expand All @@ -86,15 +76,13 @@ func main() {
startSequentialBFSTime := time.Now()
fmt.Println("Starting Sequential BFS...")

path2, _ := SequentialBFS(Maze.Cells, 10, 10)
SequentialBFS(Maze.Cells, 100, 100)
//Maze.DS.PrintArrayValues()

elapsedSequentialBFSTime := time.Since(startSequentialBFSTime)

fmt.Printf("Sequential BFS took %s\n", elapsedSequentialBFSTime)
fmt.Printf("Sequential BFS took %s\n\n", elapsedSequentialBFSTime)

printMaze(Maze, path)

for i := range path {
fmt.Printf("Path Cell %d: (%d, %d)\n", i, path[i].Row, path[i].Col)
fmt.Printf("Path2 Cell %d: (%d, %d)\n", i, path2[i].Row, path2[i].Col)
}
}
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
Enter Readme Contents Here
We found that for our advanced program, the concurrent approach is actually slower.

This is due to the backend management that has to occur when managing concurrent programs, ex mutexes and synchronization.

Our concurrent approach would have yielded better results if there were more than 4 branching paths per node. This is where bfs really excels.


Loading