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

  • Home
  • SEARCH
  • 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 8407673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:25:51+00:00 2026-06-09T23:25:51+00:00

I am reading a book by Robert Sedwick Algorithms in C++. Following is example

  • 0

I am reading a book by Robert Sedwick Algorithms in C++. Following is example given in book regarding compound data structures.

Problem statement:
Given “d”, we want to know how many pairs from a set of N points in the unit square can be connected by a straight line of length less than “d”.

Following program using the logic divides the unit squre into a grid, and maintains a two dimensional array of linked lists, with one list corresponding to each grid square. The grid is chosen to be sufficiently fine that all points within distance “d” are either in the same grid square or an adjacent one.

My questions are

  1. Why author is allocating G+2 in malloc2d(G+2, G+2) ?
  2. In gridinsert function why author is performing following statement int X = x*G+1; int Y = y*G+1; ?
  3. In for loop why we are having i intialiazed to X-1 and j initialized to Y-1?
  4. Where in code we are maintaining points within distance d in same grid square or an adjacent one?

Request your help with simple example in understanding the following progam.

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;


float randFloat() {
    return 1.0*rand()/RAND_MAX;
}


struct myPoint {
    float x;
    float y;
};

float myDistance(myPoint a, myPoint b) {
    float dx = a.x - b.x, dy = a.y - b.y;
    return sqrt(dx*dx + dy*dy);
}

struct node {
    myPoint p; node *next; 
    node(myPoint pt, node* t) {
        p = pt; next = t;
    }
};

typedef node *link;
static link **grid = NULL; 

link **malloc2d(int r, int c) {
    link **t = new link*[r];
    for (int i = 0; i < r; i++) {
      t[i] = new link[c];
    }
    return t;
 }

static int G, cnt = 0; 
static float d;

void gridinsert(float x, float y) {
    int X = x*G+1;
    int Y = y*G+1;
    myPoint p;
    p.x = x; p.y = y;
    link s, t = new node(p, grid[X][Y]);
    for (int i = X-1; i <= X+1; i++)
      for (int j = Y-1; j <= Y+1; j++)
        for (s = grid[i][j]; s != 0; s = s->next)
           if (myDistance(s->p, t->p) < d) cnt++; 

    grid[X][Y] = t;
 }

int main(int argc, char *argv[]) { 

    int i; 
    int N = 10;
    d = 0.25;
    G = 1/d;

    grid = malloc2d(G+2, G+2);
    for (i = 0; i < G+2; i++)
        for (int j = 0; j < G+2; j++)
            grid[i][j] = 0;

    for (i = 0; i < N; i++)
        gridinsert(randFloat(), randFloat());

    cout << cnt << " pairs within " << d << endl;

   return 0;
 }
  • 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-06-09T23:25:53+00:00Added an answer on June 9, 2026 at 11:25 pm
    1. The idea is to check all adjacent cells of grid. But border cells don’t have adjacents. So to avoid tricky bounds checking we extend grid by 2 extra cells – before first cell and after last one. These cells are “dummy” and will never contain any points – they are required just to simplify algorithm and provide adjacents for border cells.

    2. (X,Y) – coordinates (indexes) of cell in grid containing this point. According to p.1 we have to start placing points from cell (1,1), not (0,0). (0,0) and any other border point is dummy.

    3. Because we check all adjacent cells of grid. Adjacent cells for (X,Y) are (X-1,Y-1), (X, Y-1), (X+1, Y-1) etc to (X+1,Y+1). That’s why we have loops from X-1 to X+1 and Y-1 to Y+1.

    4. We don’t maintain them, just checking any input point vs existing set and increment counter cnt each time it matches the distance. Keeping the list of such pairs is not required by problem conditions. If you need to keep the list of points, you shall modify gridinsert() and e.g. place (s->p, t->p) to some container inside the loops instead of increment cnt++.

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

Sidebar

Related Questions

I am reading Algorithms book written by Robert Sedwick. Note: s is source and
I am reading about queues in robert sedwick book on algorithms When the items
Reading a book I have found the following statement: (Object) Behaviors answer either of
I'm reading a book about data structures in java, and it's talking about iterators
I am reading about network flow algorithms in Robert Sedwicks book on Graph algorithms.
I'm reading the book Algorithms and they mentioned a data type Item . However,
So I'm reading Robert Sedgewick's Algorithms 4th ed. book and the methods for finding
While reading a book about JavaScript I stumbled across an example: var names =
I'm reading the book 'Beginning F#', There's a short list for example code, to
I'm reading a book called Introduction to algorithms . I think many of you

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.