How to resolve the algorithm Topological sort step by step in the VBScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Topological sort step by step in the VBScript programming language

Table of Contents

Problem Statement

Given a mapping between items, and items they depend on, a topological sort orders items so that no item precedes an item it depends upon. The compiling of a library in the VHDL language has the constraint that a library must be compiled after any library it depends on. A tool exists that extracts library dependencies.

Write a function that will return a valid compile order of VHDL libraries from their dependencies.

Use the following data as an example:

Note: the above data would be un-orderable if, for example, dw04 is added to the list of dependencies of dw01.

There are two popular algorithms for topological sorting:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Topological sort step by step in the VBScript programming language

Source code in the vbscript programming language

class topological
	dim dictDependencies
	dim dictReported
	dim depth
	
	sub class_initialize
		set dictDependencies = createobject("Scripting.Dictionary")
		set dictReported = createobject("Scripting.Dictionary")
		depth = 0
	end sub
	
	sub reset
		dictReported.removeall
	end sub
	
	property let dependencies( s )
		'assuming token tab token-list newline
		dim i, j ,k
		dim aList
		dim dep
		dim a1
		aList = Split( s, vbNewLine )
		'~ remove empty lines at end
		do while aList( UBound( aList ) ) = vbnullstring
			redim preserve aList( UBound( aList ) - 1 )
		loop

		for i = lbound( aList ) to ubound( aList )
			aList( i ) = Split( aList( i ), vbTab, 2 )
			a1 = Split( aList( i )( 1 ), " " )
			k = 0
			for j = lbound( a1) to ubound(a1)
				if a1(j) <> aList(i)(0) then
					a1(k) = a1(j)
					k = k + 1
				end if
			next
			redim preserve a1(k-1)
			aList(i)(1) = a1
		next
		for i = lbound( aList ) to ubound( aList )
			dep = aList(i)(0)
			if not dictDependencies.Exists( dep ) then
				dictDependencies.add dep, aList(i)(1)
			end if
		next
		
	end property
	
	sub resolve( s )
		dim i 
		dim deps
		'~ wscript.echo string(depth,"!"),s
		depth = depth + 1
		if dictDependencies.Exists(s) then
			deps = dictDependencies(s)
			for i = lbound(deps) to ubound(deps)
				resolve deps(i)
			next
		end if
		if not seen(s) then
			wscript.echo s
			see s
		end if
		depth = depth - 1
	end sub

	function seen( key )
		seen = dictReported.Exists( key )
	end function
	
	sub see( key )
		dictReported.add key, ""
	end sub
	
	property get keys
		keys = dictDependencies.keys
	end property
end class

dim toposort
set toposort = new topological
toposort.dependencies = "des_system_lib	std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee" & vbNewLine & _
	"dw01	ieee dw01 dware gtech" & vbNewLine & _
	"dw02	ieee dw02 dware" & vbNewLine & _
	"dw03	std synopsys dware dw03 dw02 dw01 ieee gtech" & vbNewLine & _
	"dw04	dw04 ieee dw01 dware gtech" & vbNewLine & _
	"dw05	dw05 ieee dware" & vbNewLine & _
	"dw06	dw06 ieee dware" & vbNewLine & _
	"dw07	ieee dware" & vbNewLine & _
	"dware	ieee dware" & vbNewLine & _
	"gtech	ieee gtech" & vbNewLine & _
	"ramlib	std ieee" & vbNewLine & _
	"std_cell_lib	ieee std_cell_lib" & vbNewLine & _
	"synopsys	"

dim k
for each k in toposort.keys
	wscript.echo "----- " & k
	toposort.resolve k
	wscript.echo "-----"
	toposort.reset
next

  

You may also check:How to resolve the algorithm Sum of a series step by step in the REXX programming language
You may also check:How to resolve the algorithm Conjugate transpose step by step in the Sparkling programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Raku programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the Swift programming language