Posted By: Maddus Mattus | Sep 17th @ 12:49 AM
page 1 of 2
Comments: 43 | Views: 702
Maddus Mattus
Maddus Mattus
Do, or do not. There is no try. - Yoda

I am searching for an c# implemnetation of an R-Tree.

 

It's for my pet project, basically I need to index appointments and see wich ones overlap a certain time period.

 

An R-Tree seems like the best option, but I can't find any for .Net,..

 

Anyone have any ideas?

AdamSpeight2008
AdamSpeight2008
The Bandito Coder

Why mess around with r-trees?

When its possible to use linq. vb.net code ftw

 


Module Module1

 Sub Main()
  Dim appos As New List(Of Appo)
  appos.Add(New Appo(New Date(2009, 12, 1, 11, 45, 0), New Date(2009, 12, 1, 12, 0, 0)))
  appos.Add(New Appo(New Date(2009, 12, 1, 12, 0, 0), New Date(2009, 12, 1, 12, 15, 0)))
  appos.Add(New Appo(New Date(2009, 12, 1, 12, 10, 0), New Date(2009, 12, 1, 12, 25, 0)))
  appos.Add(New Appo(New Date(2009, 12, 1, 12, 1, 0), New Date(2009, 12, 1, 12, 2, 0)))

' 

' The linq query that looks for overlaps.

'
  Dim overlaps = From a1 As Appo In appos From a2 In appos Select a1, a2 _
               Where (appos.IndexOf(a1) < appos.IndexOf(a2)) AndAlso _
               ( _
                ((a1.StartTime >= a2.StartTime) AndAlso (a1.FinishTime <= a2.FinishTime)) _
                OrElse _
                ((a1.StartTime < a2.StartTime) AndAlso (a1.FinishTime > a2.StartTime)) _
                OrElse _
                ((a1.StartTime > a2.StartTime) AndAlso (a1.StartTime < a2.StartTime) _ 

AndAlso (a2.FinishTime >= a2.FinishTime)) _
                )

  For Each overlap In overlaps
   Console.WriteLine(overlap.a1)
   Console.WriteLine("overlaps")
   Console.WriteLine(overlap.a2)
   Console.WriteLine()
  Next
 End Sub

End Module
Public Class Appo
 Private m_st As Date
 Private m_et As Date

 Public Sub New(ByVal st As Date, ByVal et As Date)
  m_et = et : m_st = st
 End Sub
 Public ReadOnly Property du() As TimeSpan
  Get
   Return m_et.Subtract(m_st)
  End Get
 End Property
 Public ReadOnly Property StartTime() As DateTime
  Get
   Return m_st
  End Get
 End Property
 Public ReadOnly Property FinishTime() As DateTime
  Get
   Return m_et
  End Get
 End Property
 Public Overrides Function ToString() As String
  Return String.Format("[{0} - {1}]", m_st, m_et)
 End Function
End Class

 

edited to make code stay within lines.

TommyCarlier
TommyCarlier
I want my scalps!

What about this:

- A = all appointments, sorted by StartTime

- For each appointment A(i):

    - if A(i).EndTime >= A(i+1).StartTime then A(i) and A(i+1) overlap

The fun thing about programming languages is that they let you implement your own objects. If you can't find an R-Tree that someone has made, then make your own.

AdamSpeight2008
AdamSpeight2008
The Bandito Coder

I bow down to your superior solution. i never would have thought of sorting it first.

AdamSpeight2008
AdamSpeight2008
The Bandito Coder

Your algorithm in 2 lines.


Dim ovlap = (From a1 As Appo In appos Order By a1.StartTime)
 Dim o = ovlap.Select(Of ova)(New Func(Of Appo, Integer, ova) _
    (Function(a As Appo, i As Integer) _
      If(i > 0 AndAlso _
       ovlap(i - 1).FinishTime >= ovlap(i).StartTime, _
       New ova With {.A1 = ovlap(i - 1), .A2 = ovlap(i)}, _
       Nothing))).Where( _
       New Func(Of ova, Boolean) _

       (Function(oa As ova) (oa.A1 IsNot Nothing) AndAlso (oa.A2 IsNot Nothing)))

Need a helper structure.


Public Structure ova
 Public A1 As Appo
 Public A2 As Appo
End Structure

 

AdamSpeight2008
AdamSpeight2008
The Bandito Coder

It needs to be sorted by FinishTime not starttime

AdamSpeight2008
AdamSpeight2008
The Bandito Coder

Can the appointments span multiple days? Say 2300 --> 0100

exoteric
exoteric
I : Next<I>

Still, out of curiosity

- what is a large number

- what is slow

 

Just curious here Smiley

exoteric
exoteric
I : Next<I>

And yes, if you don't see it for C#, there is a high chance it exists for Java. I don't know about R-Trees, but Google appears to have some Java references. (And actually, do you really need a C# version?)

AdamSpeight2008
AdamSpeight2008
The Bandito Coder

In my original it at least (n^2)) * (2( n Log 2))  * => O(n^2)

Approx. itl depends on the algorithm used for IndexOf( )

 

The second is (n^2+n)/2 => O(n^2)

exoteric
exoteric
I : Next<I>

Possibly there could be a COBOL.NET version out there heh

Dr Herbie
Dr Herbie
Horses for courses

You do realise that you've just publicly stated that you're going to get involved in advanced data structures and algorithms 'just for fun'?  Now the world has proof of your nerdiness. Big Smile

 

If you progress will with the R-Tree in .NET will you be be setting up another codeproject project for it as a separate entity from the scheduler? I'm sure people could find other uses for it.

 

Herbie

 

You might want to take a look at Perst, an open source, dual license embedded database for .NET (and Java).  It includes an implementation of the R-Tree index.  http://www.mcobject.com/perst/

 

staceyw
staceyw
Before C# there was darkness...

Why does "open source" then "dual license" right next to it, sound funny to me?  Isn't that like the duality of man - war and peace.

TommyCarlier
TommyCarlier
I want my scalps!

I don't like that expression. Beer is rarely free. We should say "free as in air".

exoteric
exoteric
I : Next<I>

Air is not free. It is trapped in the atmosphere. And so, nothing is absolutely free.

page 1 of 2
Comments: 43 | Views: 702
Microsoft Communities