Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 373879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:21:03+00:00 2026-05-12T14:21:03+00:00

I have a graph with an s and t vertex that I need to

  • 0

I have a graph with an s and t vertex that I need to find the shortest path between. The graph has a lot of special properties that I would like to capitalize on:

  • The graph is a DAG (directed acyclic graph).
  • I can create a topological sort in O(|V|) time, faster than the traditional O(|V + E|).
  • Within the topological sort, s is the first item in the list and t is the last.

I was told that once I have a topological sort of the vertices I could find the shortest path faster than my current standard of Dijkstra’s Uniform Cost, but I cannot seem to find the algorithm for it.

Pseudo code would be greatly appreciated.

EDITS:
All paths from s to t have the same number of edges.
Edges have weights.
I am searching for lowest cost path.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:21:03+00:00Added an answer on May 12, 2026 at 2:21 pm

    I’m going to go against my intuition and assume this isn’t homework. You have to take advantage of the information that a topological ordering gives you. Whenever you examine the node n in a topological ordering, you have the guarantee that you’ve already traversed every possible path to n. Using this it’s clear to see that you can generate the shortest path with one linear scan of a topological ordering (pseudocode):

    Graph g
    Source s
    top_sorted_list = top_sort(g)
    
    cost = {} // A mapping between a node, the cost of its shortest path, and 
              //its parent in the shortest path
    
    for each vertex v in top_sorted_list:
      cost[vertex].cost = inf
      cost[vertex].parent = None
    
    cost[s] = 0
    
    for each vertex v in top_sorted_list:
       for each edge e in adjacensies of v:
          if cost[e.dest].cost > cost[v].cost + e.weight:
            cost[e.dest].cost =  cost[v].cost + e.weight
            e.dest.parent = v
    

    Now you can look up any shortest path from s to a destination. You’d just need to look up the destination in the cost mapping, get it’s parent, and repeat this process until you get a node whose parent is s, then you have the shortest path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an undirected graph on matris by vertex adjacency relations like that; /*
In Python, I have a Graph class that has a dictionary of vertex objects.
I have an acyclic directed graph. I would like to assign levels to each
I have a graph with Edge E and Vertex V , I can find
I have a graph data structure that I'd like to encode with protocol buffers
I have class Vertex{ Graph _graph; float x; float y; string key; //and some
I have oriented graph. Graph can be strongly connected. Every vertex can have a
I have Vertex template in vertex.h. From my graph.h: 20 template<class edgeDecor, class vertexDecor,
I have a graph representing users and some articles they wrote. I need to
I have a graph that contains nodes (groups) composed by inner nodes and when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.