Last active
August 29, 2015 13:57
-
-
Save nmische/9769346 to your computer and use it in GitHub Desktop.
Chain a list of tuples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We can sort using a custom adjacency index that allows us to pick the starting node. | |
tickets = [('PHL','NYC'),('IAD','PHL'),('SFO','IAD'),('NYC','SFO')] | |
sortedTickets = [] | |
index = {} | |
for t in tickets: | |
if t[0] not in index: | |
index[t[0]] = t | |
def getNext(ticket): | |
return index[ticket[1]] | |
currentTicket = tickets[0] | |
while len(tickets): | |
sortedTickets.append(currentTicket) | |
tickets.remove(currentTicket) | |
currentTicket= getNext(currentTicket) | |
print sortedTickets | |
#[('PHL', 'NYC'), ('NYC', 'SFO'), ('SFO', 'IAD'), ('IAD', 'PHL')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We can sort using a custom adjacency index that allows us to pick the starting node. | |
tickets = [('PHL','NYC'),('IAD','PHL'),('SFO','IAD'),('NYC','SFO')] | |
sortedTickets = [] | |
index = {} | |
for t in tickets: | |
if t[0] not in index: | |
index[t[0]] = {} | |
index[t[0]]['src'] = t | |
if t[1] not in index: | |
index[t[1]] = {} | |
index[t[1]]['dest'] = t | |
def getNext(ticket): | |
return index[ticket[1]]['src'] | |
def getPrev(ticket): | |
return index[ticket[0]]['dest'] | |
currentTicket = tickets[0] | |
while len(tickets): | |
sortedTickets.append(currentTicket) | |
tickets.remove(currentTicket) | |
currentTicket= getNext(currentTicket) | |
print sortedTickets | |
#[('PHL', 'NYC'), ('NYC', 'SFO'), ('SFO', 'IAD'), ('IAD', 'PHL')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We can sort using a comparison function but we have no control over starting node. | |
tickets = [('PHL','NYC'),('IAD','PHL'),('SFO','IAD'),('NYC','SFO')] | |
def compareTix(a,b): | |
if a[0] == b[1]: | |
return 1 | |
if a[1] == b[0]: | |
return -1 | |
return 0 | |
tickets.sort(cmp=compareTix) | |
print tickets | |
#[('NYC', 'SFO'), ('SFO', 'IAD'), ('IAD', 'PHL'), ('PHL', 'NYC')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
# Using a graph we can find the cycle for a given start node. | |
G=nx.Graph([('PHL','NYC'),('IAD','PHL'),('SFO','IAD'),('NYC','SFO')]) | |
print list(nx.cycle_basis(G,'PHL')) | |
# [['NYC', 'SFO', 'IAD', 'PHL']] | |
# Using a directed graph we can find the cycles, but can't pick the start node. | |
DG=nx.DiGraph([('PHL','NYC'),('IAD','PHL'),('SFO','IAD'),('NYC','SFO')]) | |
print list(nx.simple_cycles(DG)) | |
# [['NYC', 'SFO', 'IAD', 'PHL']] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment