Efficiently determining the K shortest paths in a graph

My goal is to efficiently find the $ k$ shortest paths between a source and a destination in an undirected graph. I implemented two solutions of this problem myself, but, as I am very interesting in efficiency, was wondering if there might be a more efficient solution to this problem.

The first solution is based on Yen’s algorithm (https://en.wikipedia.org/wiki/Yen%27s_algorithm):

yen[graph_, source_, destination_, k_] :=    Module[{a, b, gtemp, spurnode, roothpath, sp, roothminusspur,      double, WAmatrix, dirgraph},    a = {FindShortestPath[graph, source, destination]};    b = {};    Do[     Do[      gtemp = graph;      roothpath = a[[-1]][[1 ;; i + 1]];      roothminusspur = Drop[roothpath, -1];      double =        Table[If[         a[[l]][[1 ;; Min[i + 1, Length[a[[l]]]]]] == roothpath,          a[[l]][[i + 1]] \[UndirectedEdge] a[[l]][[i + 2]], {}], {l, 1,          Length[a]}];      gtemp = EdgeDelete[gtemp, Union[Flatten@double]];      gtemp = VertexDelete[gtemp, roothminusspur];      sp = FindShortestPath[gtemp, roothpath[[-1]], destination];      If[Length[sp] > 0,        AppendTo[        b, {GraphDistance[gtemp, roothpath[[-1]], destination],          Flatten@{roothminusspur, sp}}]];      , {i, 0, Length[a[[-1]]] - 2}];     If[Length[b] == 0, Break[],       b = SortBy[Union[b], First];      AppendTo[a, b[[1]][[2]]];      b = Drop[b, 1]];     , {j, 1, k - 1}];    Return[a]    ]; 

The second solution is a bit ugly and can be arbitrary slow, but works quite well on graphs that have a lot of arcs and the weights between these arcs do not differ that much. The idea is to use the build-in FindPath function of Mathematica and increase the costs, until you have indeed found $ k$ or more paths. If you have found more than $ k$ paths, you delete the paths with the most costs:

nmatrix = WeightedAdjacencyMatrix[graph]; maxcosts = Total[nmatrix, 2]; costs = GraphDistance[graph, source, destination]; Do[  paths = FindPath[graph, source, destination, costs + l, All];  If[Length[paths] >= k, costest = costs + l - 1; Break[]],   {l, 0, Round[maxcosts - costs]}]; If[Length[paths] > k,   defpaths = FindPath[graph, source, destination, costest, All];  possiblepaths = Complement[paths, defpaths];  costpaths =    Table[Sum[     nmatrix[[possiblepaths[[j]][[i]]]][[possiblepaths[[j]][[i +           1]]]], {i, Length[possiblepaths[[j]]] - 1}], {j,      Length[possiblepaths]}];  paths = Join[defpaths,     possiblepaths[[Ordering[costpaths][[1 ;; k - Length[defpaths]]]]]];  ]; 

Any hints/suggestions for speed-up techniques or more elegant solutions are more than welcome 🙂

determining decidability with intersection of context free languages

I am trying to solve this problem: Given context-free languages A, B, and C find if the language $ (A\cap B)\cup (B\cap C)$ is empty. Is this problem decidable.

I know the CFG is not closed under intersection so we do not know that $ A\cap B$ is also CFG. If it was CFG, I know how to prove decidability. Since we can’t determine about $ A\cap B$ , is there a way to prove whether or not this is decidable?

Determining sample size of sectors in order to validate a data wipe procedure

I am doing some research into validating implementations of hardware optimized data wipe procedures in solid state storage devices, such as ATA8 Security Mode Erase and NVMe Secure Erase procedures.

As I have attempted to define what "success" means in this context I have established that a key measure would be that "it is possible to demonstrate a change in the value of a sector X of the storage medium between observations pre and post wipe."

The most rigorous approach to this would be to make a copy of all of the sectors, conduct the wipe procedure, then compare every sector’s new value with the reference copy and ensure that it is different. However this extremely time consuming and only really practical in a lab environment.

At the opposite extreme, simply checking that the initial sectors of the medium where the file-system structures are held are no long valid is not sufficient as the actual data is easily recoverable in their absence.

The middle ground then appears to be record a number of observations of sectors randomly selected from the medium, conduct the wipe, then compare. I believe the key to that is to determine in some formal fashion what how many sectors to sample in order for there to be any confidence in the outcome.

My understanding of sampling theory from college is all based upon sampling human populations using established models and tables, which I don’t think apply here. Accordingly, I am looking for suggestions as to techniques that can be applied to determine an appropriate sample size, or if due to the nature of the population it is not possible to actually construct such a sample with any useful meaning. I think I understand that statistical models rely upon the ability to reason about other people you didn’t observe based upon those you did, and it’s not clear to me that in this case there is a way to reason about the state of other sectors based upon the ones you check. If that were the case than perhaps all you are left with is making some arbitrary decision that X percentage of sectors being wiped is sufficient according to some policy standard, but that feels unsatisfactory to me.

This might be a Statistics question rather than a Computer Science question, but I am more comfortable with CS terminology that stats, and I think an understanding of how storage devices work is important to understanding the question, so I decided to start here. If this would be better off asked elsewhere please let me know.

Determining Genie Style AoE damage


Shaitan Earthblast (Combat)

Benefit: While using the Shaitan Style feat, as a standard action, you can spend two Elemental Fist attempts to unleash a 20-foot column of acid that has a 5-foot radius and erupts from a point of origin within 30 feet of you. Creatures caught in the column take your unarmed strike damage plus the acid damage from your Elemental Fist and are staggered for 1 round. A successful Reflex save (DC 10 + 1/2 your character level + your Wis modifier) reduces the damage by half and prevents a target from being staggered

I’d like some clarification on what does and doesn’t affect “unarmed strike damage”:

  • Bonuses from class skills? (such as a Fighter’s Weapon Training)
  • Enhancement bonus from magic items? (such as a +2 Amulet of Mighty Fists)
  • Special abilities from magic items? (such as a Shock Amulet of Mighty Fists)
  • Damage or other effects from feats? (Weapon Specialization, Power Attack, Bleeding Attack, etc.)

Obviously this question extends to all the Genie Style AoE attacks.

Determining “level” of an NPC ally for purpose of budgeting encounter XP

Hypothetical party:

1st level human wizard (PC)
3x guard NPC
1x acolyte NPC

This is the player’s first time, and it’s a solo campaign. The first mini-adventure involves recovering the cargo of a supply caravan that didn’t make it to her magic academy due to bandit hijacking (as a sort of graduate-test). The guards and healer are on contract from the school to assist her. The acolyte is essentially just to hang back and tend to wounds and probably won’t engage in combat unless someone goes down or the PC is seriously wounded.

So, with that “party” makeup, how do I determine the XP budget for a “medium” or “hard” encounter since there’s no clear CR->level mapping in 5e for those NPCs (who will certainly factor into any combat as meatshields and melee damage)? I was thinking about reverse-engineering them as crumby lvl2 fighters (given the 2 HD worth of HP they have), but they certainly aren’t on par with a lvl2 fighter, given the lack of action surge, second wind, etc., so the encounters would end up being too hard compared to PCs or DMPCs.

Could they be lvl1-equivalents, essentially trading any abilities a PC would have for extra HP? How would this work at later levels with higher CR NPCs (veteran, priest, mage, etc.)? I’d rather not have to build a bunch of DMPCs as hirelings, because I want the NPCs to be a bit vanilla compared to the versatility of my player. I’m already introducing 1-2 DMPCs to the game for her to take control over as she gets a feel for the flow of the game (for now, she’ll handle them out of combat, and I’ll effectively show her how to utilize them until she’s ready).


Just to be clear, I’m comfortable with the monster-generation rules for crafting NPCs as enemies to determine their CR, but I’m confused on how to use them in the adventuring party when budgeting encounter difficulty XP.

Does hurling a thrown weapon change the weapon’s category for determining proficiency?

On Weapon Categories says, “A melee weapon with the heavy thrown or the light thrown property counts as a ranged weapon when thrown” (Player’s Handbook 215). (Similar text is absent from the Rules Compendium, but I’m not familiar enough with 4e to know if this absence is an issue.)

With this in mind, if a character who has proficiency with military melee weapons but lacks proficiency with military ranged weapons throws a handaxe—a military weapon—, does the character still receive his proficiency bonus on the attack roll?

That is, when a military melee weapon is thrown does the military melee weapon remain a melee military weapon therefore the character above gains his proficiency bonus with it? Or does the thrown military melee weapon now count as a military ranged weapon therefore the character above does not gain his proficiency bonus with it? Or is this even more—or less!—complex than I’m imagining?


Note: My level 3 battlemind who has Strength 10, Dexterity 12, proficiency with simple and military ranged weapons, and proficiency only with simple ranged weapons will still normally make his ranged basic attacks with daggers—preferring accuracy to damage—, but I want to assess my future options just in case. Also, I know I’m late to the party, and I apologize if this is a topic that’s already been discussed to death elsewhere.

What determining the DC to notice an invisible enemy during combat, what counts as “in combat” or “not moving”?

What modifiers should be applied when when determining the Perception DC to locate an invisible creature during common combat situation?

As per the Core Rulebook, the base DC is 20, and to pinpoint the exact square is another +20. There are various additional modifiers on this DC, but three that are of particular relevance to me are:

  • -20 if the creature is in combat or speaking
  • +20 if the creature is not moving
  • -10 if the creature is moving at full speed

I’m curious exactly what constitutes as “in combat” or “not moving”, as I can’t seem to get a consistent answer from the rules or various discussion threads on invisibility.

Let me illustrate with a few scenarios, where we assume that a PC has cast Greater Invisibility on themselves, and a few rounds later, has attacked NPC 1. Later that round, NPC 2 (who was not attacked by the PC), wants to locate the invisible PC.

  • Scenario 1: The PC begins their turn adjacent to NPC 1 and spends a full round action to attack NPC 1 before ending their turn with no other actions performed (thus did not take a move action and remained in their square). NPC 2 now tries to locate the PC.

  • Scenario 2: As above, but this time the PC takes a 5-foot-step away from NPC 1 in an unknown direction after attacking. NPC 2 now tries to locate the PC.

  • Scenario 3: As above, but this time the PC only uses a standard action to attack NPC 1, before taking a move action to move at their full speed away from NPC 1 in an unknown direction. NPC 2 now tries to locate the PC.

And finally, for a slightly different scenario from above

  • Scenario 4: The PC is no longer performing melee attacks, and is not standing adjacent to any enemies. Instead, they begin their turn by taking a potion and drinking it. NPC 2 now tries to locate the PC.

Which modifies would you apply in each scenario? My interpretation of the DC to pinpoint the exact square would be

  • Scenario 1: In Combat, Not Moving => DC = 40 – 20 + 20 = 40
  • Scenario 2: In Combat => DC = 40 – 20 = 20
  • Scenario 3: In Combat, Moving at full speed => DC = 40 – 20 – 10 = 10
  • Scenario 4: In Combat, Not Moving => DC = 40 – 20 + 20 = 40

However I’m unsure of the modifiers in italics are applied correctly.

Does Not Moving here specifically refer to taking a move action, or is it anything other than standing completely still? Is swinging a weapon equivalent to drinking a potion when it comes to this?

Does In Combat specifically refer to having rolled initiative as part of an encounter? Or does it mean actively participating with aggressive actions?

Any help clarifying RAW or RAI would be appreciated!

(Note: I’m aware that in these combat situations, NPC 1 can easily locate the PC as they were the target of an attack. I’m also aware that invisibility only removes vision of the invisible creature, and that the PC could be located by other means – including environmental factors or being told by an ally to target a particular square. I’m specifically interested in the scenario where there’s no additional stimulus, and another NPC is rolling perception to pinpoint the PC’s location)

Determining Volume of an Irregularly Shaped Triangular Prism

I need help using Mathematica to determine the volume of an irregularly shaped triangular prism. I have very little experience with Mathematica but thought this task could easily be performed using the program.

The shape generated should look like:

Graphics3D[

Prism[{{3.767, 4.523, 1}, {0, 0, 0}, {7.948, -2.451, 0}, {3.699,

8.156, 1}, {1.146, 10.24, 0}, {4.837, 10.24, 0}}]]

I’ve tried using the Volume[] function but with no luck. Any help would be greatly appreciated!