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 7695153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:29:02+00:00 2026-05-31T21:29:02+00:00

I have to write an algorithm that find the path in DAG with single

  • 0

I have to write an algorithm that find the path in DAG with single source and single sink…
I really don’t know what to do ( tried to use DSF and topological sort but nothing…)
I don’t want anyone to solve it for me but just some guidance.

Thank you

  • 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-31T21:29:03+00:00Added an answer on May 31, 2026 at 9:29 pm

    BFS finds shortest path in general unweighted graph – and also specifically in DAG [since it is also a graph]. It is also pretty simple to code it.

    Note that DFS will find a path – but it doesn’t have to be the shortest one.

    You might also want to have a look at this post to see how to get the actual path from BFS after running it.

    EDIT: according to your comments, it seems you want O(|V|) solution, and it doesn’t have to be shortest. a modification of DFS is the way to do then, since the DAG has a single source and a single sink, every path from the source reaches the sink.

    Note that since your graph is a DAG and since we established that every path from the source reaches the sink, you don’t need to go backward after exploring a certain path [so no recursion or stack is needed].

    pseudo-code:

    modifiedDFS(source,target):
      map <- new map
      current <- source
      while (current != target):
        next <- current.getNextVertex() //just chose an arbitrary edge and follow it, doesn't matter which
        map.put(next,current) //we "discovered" next by following current
        current <- next
    

    after running this algorithm, you need to follow back the map from the target to the
    source – and you get the actual path [reversed of course]

    The complexity is indeed O(n) because we visit each node at most once. In order to maintain O(n) the map has to be a hashmap [and not a treemap]. If the vertices are enumerated – you can even implement the map as an array.

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

Sidebar

Related Questions

I have to write something in vbscript that need to use a unique set.
The debug screen says that django does not have write access to the db.
I'm writing a simple web app in PHP that needs to have write access
qichunren@zhaobak:~> gem install hpricot ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write
I have to write an applet that brings up a password dialog. The problem
I have to write a component that re-creates SQL Server tables (structure and data)
Below is the problem description and algorithm that I have written. Is there anything
I'm writing an application that uses Dijkstra algorithm to find minimal paths in the
I want to write a generic averaging algorithm. That is, for any type T
I am attempting to write (or expand on an existing) graph search algorithm that

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.