FreeExam

Graphs and Dependency Algorithms

Thirty original Python problems on graph identity, dependency support, constrained routes, cycle evidence, graph cuts and exact finite optimization. Preserve edge occurrences, missing support and full witness ties rather than relying on input order.

These problems are original practice. They are not questions from a named employer or a live hiring loop.

  1. 1.Preserve loop degree and incident edge identity

    Write incidence_table(nodes,edges), unique node names, undirected [unique_id,u,v] edges with declared endpoints. Return sorted [node,degree,sorted_incident_ids]. A loop contributes TWO to degree but its ID appears once in that node list. Parallel edges retain separate IDs; isolated nodes remain. No mutation.

  2. 2.Stop dependency expansion at installed support

    Write dependency_frontier(dependencies,requested,installed). Dependencies maps declared task to prerequisite names. Traverse from requested; installed names satisfy support and STOP traversal even if declared. Other reachable declared names are needed; undeclared names are external, not tasks. Cycles terminate by visited names. Return [sorted_needed,sorted_external], deduplicated. No mutation.

  3. 3.Canonicalize strong components and quotient edges

    Write strong_groups(nodes,edges), directed [u,v] edges with declared endpoints. Return [components,condensation_edges]. Each component contains mutually reachable nodes (zero-length self reach allowed), members sorted; components sorted lexically and indexed from 0. Condensation edges are sorted distinct [source_index,target_index], omitting internal edges. Preserve isolated nodes. No mutation.

  4. 4.Separate cycle cores from their downstream blocked tasks

    Write blocked_tasks(dependencies), {task:prerequisites}; unknown prerequisite => None. Run lexical Kahn scheduling, always taking smallest ready task. Among unscheduled tasks, cyclic means lying on a directed cycle (including self-loop); others are downstream-blocked. Return [scheduled_order,sorted_cyclic,sorted_downstream]. Duplicate prerequisites collapse. No mutation.

  5. 5.Reject cyclic edge proposals with prior-path witnesses

    Write insert_acyclic(nodes,initial,proposals), directed [id,u,v], declared endpoints. Initial DAG has unique IDs. Reject active ID as id; else reject cycle if prior v->u path exists, shortest then lexical edge IDs; self witness []. Else accept. Return [accepted_ids,rejections], rejections [proposal_index,reason,witness], id witness []. Only accepted IDs reserve identity. No mutation.

  6. 6.Remove only reachability-redundant edges from a DAG

    Write reduce_dependencies(nodes,edges), directed pairs, duplicates collapse. Any cycle anywhere, including a self-loop, returns None. Otherwise return sorted retained [u,v] edges: remove a direct edge iff another path u->v exists without it. Preserve reachability and isolated nodes implicitly; do not return reflexive edges. No mutation.

  7. 7.Find a minimum-hop walk that visits every required vertex

    Write visit_route(nodes,edges,start,target,required), directed [unique_id,u,v], <=6 required names. Walk may revisit vertices/edges; visits include start and final target. Return fewest-edge walk then lexical edge-ID list satisfying all required visits, or None if impossible/unknown required name. Empty walk is allowed only if target=start and requirements already visited. No mutation.

  8. 8.Route through graph and label automaton states together

    Write automaton_route(nodes,edges,transitions,start,target,initial,accepting). Directed edges [unique_id,u,v,label,nonnegative_cost]; transitions {state:{label:next}}. Missing transition forbids traversal. Finish at target in accepting state. Return [minimum_cost,edge_IDs], ties fewest edges then lexical IDs, or None. Empty walk allowed if initial acceptance and endpoints match. No mutation.

  9. 9.Optimize route bottleneck before its total cost

    Write bottleneck_route(nodes,edges,start,target), directed [unique_id,u,v,nonnegative_weight]. Minimize (maximum edge weight,total weight,edge count,lexical edge-ID list). Return [bottleneck,total,IDs] or None. Empty route for start=target gives [0,0,[]]. A prefix with a smaller bottleneck need not have the best total after a later heavy edge equalizes bottlenecks. No mutation.

  10. 10.Keep zero-capacity routes distinct from missing routes

    Write capacity_route(nodes,edges,start,target), directed [unique_id,u,v,nonnegative_capacity]. Maximize minimum edge capacity, then minimize edge count then lexical edge IDs. Return [capacity,IDs], None if unreachable. Empty route for start=target is [None,[]], meaning unbounded empty-path capacity. A zero-capacity route is supported and differs from no route. No mutation.

  11. 11.Count parallel identities on minimum-hop routes

    Write shortest_counts(nodes,edges,start,target), directed [unique_id,u,v]. Return [minimum_edges,number_of_shortest_walks,lexical_edge_ID_witness], or None if unreachable. Parallel IDs count separately. Loops/cycles do not add minimum-hop walks; start=target has exactly one empty shortest walk. Counts are exact integers. No mutation.

  12. 12.Detect infinite minimum-cost walk counts on optimal support

    Write shortest_walk_counts(nodes,edges,start,target), directed [unique_id,u,v,nonnegative_cost]. Walks may repeat edges and visit target before their final stop. Return [minimum_cost,count_or_infinite], exact count or string infinite, None if unreachable. A zero-cost cycle makes the count infinite only if it lies on a minimum-cost start-target walk. Empty walk is one candidate. No mutation.

  13. 13.Find bridges while preserving parallel parent edges

    Write bridge_ids(nodes,edges), undirected [unique_id,u,v], declared endpoints. Return sorted IDs whose individual removal increases the whole graph component count. Loops never qualify; parallel edges are separate identities and can protect each other. Preserve disconnected components in the analysis. No mutation.

  14. 14.Report component-local split sizes after a vertex disappears

    Write split_footprints(nodes,edges), undirected pairs, duplicates/loops allowed. For each removed vertex, consider ONLY its original connected component minus that vertex. If it splits into >=2 nonempty pieces, report [vertex,sorted_piece_sizes]. Return reports by vertex. Unrelated components and isolates do not count as split pieces. No mutation.

  15. 15.Group edge identities by shared simple-cycle support

    Write cycle_blocks(nodes,edges), undirected [unique_id,u,v]. Nonloop edges are equivalent if linked transitively by co-occurrence on a vertex-simple cycle. Two parallel edges form a length-2 cycle. Edges on no cycle are singleton blocks; each loop is its own singleton block. Return sorted blocks of sorted edge IDs. Isolates create no block. No mutation.

  16. 16.Solve graph parity equations with fixed vertex bits

    Write parity_labels(nodes,edges,pins), undirected [u,v,bit_0_or_1] enforcing label[u] XOR label[v]=bit. Pins maps declared nodes to fixed 0/1 bits. Return lexical smallest bit list in sorted node order, or None if inconsistent. Isolated unpinned nodes choose 0; loops and duplicate constraints must be checked. No mutation.

  17. 17.Find a shortest odd-parity closed edge witness

    Write parity_cycle(nodes,edges), undirected [unique_id,u,v,xor_bit]. Find a closed walk with XOR 1; minimize edge count then lexical edge-ID sequence over ALL starting vertices/directions. Edges may repeat, but empty walk has XOR 0. Return edge-ID list or None. Odd loop qualifies; parallel identities may form a two-edge contradiction. No mutation.

  18. 18.Find a maximum incomparable task set with canonical ties

    Write maximum_antichain(nodes,edges), <=12 nodes, directed dependencies [u,v]. Any cycle anywhere => None. Otherwise return largest node subset with no positive-length path between any two distinct members in either direction; ties lexical sorted member list. Isolated nodes can join every incomparable set. Empty DAG gives []. No mutation.

  19. 19.Disconnect routes with a protected minimum-cost vertex cut

    Write vertex_cut(nodes,edges,start,target,costs,protected), directed pairs, <=12 costed vertices, nonnegative integer costs. Only costed unprotected vertices other than endpoints may be removed. Minimize (cost,removed_count,lexical_sorted_removed). Return [cost,removed] or None if impossible. Already disconnected gives [0,[]]; start=target cannot be disconnected. No mutation.

  20. 20.Find edge identities present on every supported route

    Write mandatory_edges(nodes,edges,start,target), directed [unique_id,u,v]. Return sorted IDs that occur on EVERY start-target walk, or None if no route exists. Parallel edges are distinct. Empty walk when start=target makes the answer []; cycles do not change the definition. Do not infer vacuous domination for unreachable target. No mutation.

  21. 21.Retain every incomparable lowest common dependency ancestor

    Write common_ancestors(nodes,edges,queries), DAG edges parent->child. Any cycle anywhere or unknown query => None. Ancestor includes a node itself. Return sorted common ancestors with NO different common ancestor reachable below them. Multiple incomparable lowest ancestors may remain; do not choose one by traversal order. Empty queries return []. No mutation.

  22. 22.Project graph identities and grouping-induced cycles

    Write quotient_graph(nodes,edges,groups), directed [unique_id,u,v], groups maps every node to a group name. Return [sorted_group_names,projected_edges,lexical_topological_order_or_None]. Projected rows [source_group,target_group,sorted_original_ids], sorted by group pair. Retain internal self-edges; parallel projected pairs merge IDs. Grouping may create cycles even from a DAG. No mutation.

  23. 23.Compute minimum proof depth across AND-input OR rules

    Write rule_depth(initial,rules), [unique_id,input_names,output]. Initial names have depth 0/no rule. A rule supports output at 1+max(input_depth), empty inputs depth 1. Alternative rules choose minimum depth then lexical rule ID. Unknown/unseeded cyclic support stays None. Return sorted [name,depth_or_None,rule_or_None] for every mentioned name. Duplicate inputs collapse logically. No mutation.

  24. 24.Find least stages with non-strict and strict dependency gaps

    Write least_stages(nodes,constraints), [u,v,nonnegative_integer_gap] means stage[v]>=stage[u]+gap; all stages>=0. Return sorted [node,least_stage] satisfying every constraint, or None if impossible. Zero-gap cycles are allowed; any positive-total cycle is impossible. Declared endpoints; duplicate constraints allowed. No mutation.

  25. 25.Replay graph updates without applying invalid changes

    Write connectivity_replay(nodes,operations). Initially no undirected edges. Operations [add,id,u,v], [remove,id], [ask,u,v], declared endpoints. Duplicate active ID add/missing ID remove is rejected and ignored; removed IDs may be reused. Ask returns connectivity, including each node to itself. Return [answers_in_query_order,rejected_operation_indices]. Parallel edges keep IDs. No mutation.

  26. 26.Compute exact route support under independent edge presence

    Write route_reliability(nodes,edges,start,target), <=10 directed [unique_id,u,v,num,den], 0<=num<=den, den>0. Edges present independently with probability num/den. Return reduced [numerator,denominator] probability that a start-target route exists. Parallel IDs independent; loops add no reachability. Empty self route always exists. Finite model; no real-world reliability claim. No mutation.

  27. 27.Compute exact expected steps or retain infinite hitting time

    Write absorption_steps(nodes,edges,start,target), <=6 nodes, directed [u,v,positive_integer_weight]. Non-target chooses outgoing edge proportional to weight; target absorbs. A non-target dead end stays forever. Return reduced expected steps [num,den], or None if target is not reached almost surely. Parallel rows add weight. Start=target gives [0,1]. Exact finite model only. No mutation.

  28. 28.Use every undirected edge identity exactly once

    Write edge_walk(nodes,edges,start,end), <=9 undirected [unique_id,u,v], declared endpoints. Return lexical smallest edge-ID sequence of a walk starting start, ending end, and using EVERY edge exactly once, or None. Loops consume one edge; parallel IDs remain separate. Vertices may repeat. Empty edge set succeeds only when endpoints match. No mutation.

  29. 29.Optimize bounded simple routes with signed rewards

    Write reward_path(nodes,edges,start,target,rewards), <=10 nodes, directed pairs, integer vertex rewards. Route cannot repeat a vertex. Maximize sum of visited rewards, then minimize edge count, then lexical vertex sequence. Return [reward,vertex_path] or None if unreachable. Include start/target rewards; start=target permits only its one-vertex path. Duplicate edges collapse. No mutation.

  30. 30.Remove minimum-cost edges to restore a DAG

    Write feedback_edges(nodes,edges), <=6 nodes/10 directed [unique_id,u,v,nonnegative_cost]. Remove edges to make remaining graph a DAG. Minimize (removed_cost,removed_count,lexical_sorted_removed_IDs). Return [cost,removed_IDs]. Self-loops must be removed; parallel identities are independent. Empty removal wins an already acyclic graph even with zero-cost edges. No mutation.