You are not logged in. Please login at www.codechef.com to post your questions!

×

let see how much deep u know about segmentation fault in c++

2
1

Question link - link text

solution link -link text

I want to know why i am getting segmentation fault on answer where my code is working for smaller input. It work on on dijkistra algo and code help is from geeksforgeeks code link https://www.geeksforgeeks.org/printing-paths-dijkstras-shortest-path-algorithm/

for more test cases refer to hackerrank https://www.hackerrank.com/contests/w38/challenges/a-time-saving-affair/submissions/code/1308366537

And this question form hackerrank week of code 38

asked 25 Jun '18, 16:50

gyanendra371's gravatar image

3★gyanendra371
2936
accept rate: 33%

to understand the code quickly u can visit geeksforgeeks link given

(25 Jun '18, 16:56) gyanendra3713★

Well all those who can suggest possible reason for segmentation fault are most welcome , i think it will help others to know about segmentation fault or runtime error which is hard to debug. And for any assistance just comment here i will serve it.

(25 Jun '18, 16:58) gyanendra3713★

Hi @gyanendra371,

I have check your code and the ONE MAJOR BUG in your code is that you have created the graph using adjacency matrix and not adjacency list.

The given constraint for number of nodes is 1≤N≤104 and in most of the programming languages you can allocate at most 106 to 107 memory space. But, in your code you have created the graph as long graph[n][n] , which would try to allocated 108 memory locations and hence would fail.

So, in my opinion you should learn to build graph using adjacency list.

You can check my accepted solution here.

You can ping me in case of any doubts.

Hope this helps!! Happy Coding!! :-)

link

answered 25 Jun '18, 17:28

brijwasi1995's gravatar image

2★brijwasi1995
21615
accept rate: 11%

hey can u provide reference for dijkistra algo using adjancey list which can store the shortest path @brijwasi1995 and would u mind adding little bit of comment in ur code that can help lot. And thanks for giving ur precios time .

(25 Jun '18, 17:58) gyanendra3713★
1

Hi, you can learn about adjacency list and shortest path algorithm implementation from the following links.

  1. Adjacency List - https://www.hackerearth.com/practice/algorithms/graphs/graph-representation/tutorial/

  2. Shortest path algorithm implementation - https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/tutorial/

(25 Jun '18, 18:41) brijwasi19952★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×180
×52
×18

question asked: 25 Jun '18, 16:50

question was seen: 188 times

last updated: 25 Jun '18, 18:41