Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Currently translating weighted DAG to C code which is written in Go language and topologically sorted. Actually I missed a couple part of the code that is the code below sample. In topoSort function, I couldn't get what "visit" declaration is. Is it function declaration within another function ? Another part is that in main function that you'll see it below, couldn't get slicing processes with range keyword in loops. If you translate into C syntax roughly or briefly that would be great.

// topoSort()
func (g *graph) topoSort() []int {
    result := make([]int, g.size())
    marks := make([]bool, g.size())
    resultIndex := g.size() - 1

    var visit func(int)
    visit = func(u int) {
        for _, item := range g.adjList[u] {
            if !marks[item.vertex] {
                visit(item.vertex)
            }
        }

        marks[u] = true
        result[resultIndex] = u
        resultIndex--
    }

    for u := range g.adjList {
        if !marks[u] {
            visit(u)
        }
    }

    return result
}


// main()
i := 0
	for rowIndex, row := range input {
		for _, number := range row {
			if rowIndex+1 >= len(input) { // Last row.
				g.addEdge(i, vertexesQuantity, number)
			} else { // Not the last row.
				g.addEdge(i, i + rowIndex + 1, number)
				g.addEdge(i, i + rowIndex + 2, number)
			}

			i++
		}
	}


What I have tried:

I made it and converted basic graph ( weighted DAG ) structure from that sample and there no issue about that. I just struggled with that two of them functions to convert to C code as I described above.
Posted
Updated 2-May-19 22:29pm

1 solution

There's no array slice in C. You'd have to create a new array.

Visit - The Go Programming Language[^][^]


The source code for the visit function is here, so I assume you can work out what it does from here
 
Share this answer
 
Comments
reBertran 3-May-19 4:39am    
I know there is no slicing processes in C but there is ( go code ) and didn't get it in proper way about the process above.
Christian Graus 3-May-19 4:41am    
A slice of an array is a section. Create a new array.
reBertran 3-May-19 4:43am    
Can you define that rowIndex, row and number variables in pseudo or C code in more proper way ?
Christian Graus 3-May-19 4:44am    
I can't see code in your example doing a slice. I don't understand your question
reBertran 3-May-19 4:49am    
http://xra.sh/project-euler-problems-18-and-67 there is whole code on it if you look at and didn't convert that for loops in main() into C code properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900