Posts Tagged “python”

Fantástico… Já gosto outra vez de python :)

Em vez de ter de implementar uma heap como me aconteceu em Java, o meu querido python já tem uma implementada por mim.

Mas o que é uma heap?

Simplificando uma heap é uma estrutura em árvore que tem a particularidade de ter na raiz o nó com menor ou maio valor que existe na árvore. Em adição tem custos baixos, O( ln( n ) ) , para grande parte das operações básicas, tais como adição e remoção de nós.

Mas a implementação em python é uma min-heap e eu preciso de uma max

Duhhhhh….. em vez de:
[code lang="python"]def __cmp__(self, other):
return cmp(self.getHeuristica(), other.getHeuristica())[/code]

Basta usar:
[code lang="python"]def __cmp__(self, other):
return other.getHeuristica() - self.getHeuristica() [/code]

Não li atentamente o código da heap que vem com o python, mas fiquei com a ideia que seria a clássica Heap Binária. No meu caso onde facilmente tenho algumas dezenas de milhares de nós e estou constantemente a tirar um e meter uns 20, talvez fosse interessante uma fibonacci heap……. Fica para outro dia.

Comments No Comments »

The Global Interpreter Lock is python’s way of solving the issues of multiple threads running on multiple cpu’s thrashing common data.
What happens is that only one thread runs at any time.
In the last week I’ve been implementing some search algorithms that would greatly benefit from multiple threads on multiple cpu’s. Some of you might say:

If you are using Unix or Cygwin you can use os.fork()

Yes I can, but I’m not even going to delve into the issues around Threads VS Fork in terms of the time it will take to spawn a thread or spawn another process.

In the work I was doing I would use threads to do very small (as in time) tasks, but spawning many threads in a small amount of time. Seems I’ll have to re-think the approach…..

Comments 5 Comments »