Is There a Java Class for this Kind of Data Structure?

H

Hal Vaughan

Oliver said:
Picking the right data structure for a given problem is one of those
things you just acquire through experience, I think. It's difficult for me
to say for sure, because I don't know what you're doing with your tables,
but based on what I heard so far, an acyclic directed graph sounds like
the the data structure I'd used to model the dependencies between your
tables.

I checked out the Wikipedia article on acyclic directed graphs. In some
ways that is close to what I was doing. Each table contained a list of its
dependent tables and, of course, they had lists of their dependent tables
too. I made sure they always went to the next level so no path would go to
a table on the same or a previous level.
Presumably, your program is trying to "do something": model global
warming, schedule airplane flights, track inventory in a warehouse, etc.
This "thing" that it is trying to do exists independently of your program;
that is, you could do it manually if you needed to, but it might be
painful, boring, repetitive, etc. This "thing" that is independent of your
program is your "problem domain".

It's a setting editor for another system (which is mostly in Perl, since it
handles a LOT of text). For various reasons, I don't want this program
interacting directly with the database, in part because this would be
accessible from the web and nothing else is. The settings data from the
database is converted to text in what is almost XML, but is something I set
up that made it easy to read and write the data easily and quickly from
Java and Perl, saved in files on the server, and this reads them in and
creates the tables. Basically, this program is a GUI that lets me edit the
settings quickly with point-and-click which, for me, is much faster than
dealing with all the SQL commands and the interrelations of the different
settings.
So what I'm wondering is whether the entities in your problem domain
naturally have a tree-like structure to them. If so, then it might be
worth it to stick with trees in your program that reflect the structure of
the problem domain, and use the queue-based algorithm mentioned below.
Otherwise, it might be worth chucking the trees in favour of the acyclic
directed graph (http://en.wikipedia.org/wiki/Acyclic_directed_graph).

After what you're saying, I'd say it's more in the acyclic directed graph.
I had never heard of a data structure like that before, but it fits better
than a tree.

Side note here: on some forums when I ask questions like the one I started
with, I get treated like I'm an idiot because I don't know what a lot of
people think is something basic. On this group, I've noticed that it may
happen once in a while, so I usually include that I'm self taught in my
first post. I've found that once people see that I have taught myself,
they understand why there are holes in what I know and people here often
give me a lot of info that goes way beyond answering my current question
and helps me later.

[A good explanation of acyclic directed graphs snipped for brevity.]
....
What you've written is essentially what I described as being a
breadth-first traversal using a queue (except you need not make these
sublists). I think generally, it would not be considered a form of
recursion. I think (but am not sure) that the technical definition of
recursion requires a method to call itself either directly or indirectly.

I didn't really know what recursion was until after I had created several
recursive subroutines on my own. Then I found that "recursion" described
what I had been doing. All the examples I saw were routines calling
themselves, but when I thought about it, and what I did here amounted to
something like this:

while (!isDone) {
//All sorts of stuff to be done
if (no more tables)
isDone = true;
}

It doesn't call itself, but it keeps going and repeating itself until it's
done. It does essentially the same thing as a recursive subroutine. I did
something slightly different. I used a LinkedList and each node is another
LinkedList of all the tables on a particular level that need updating, so I
did this:


private void updateTables(MyTable firstTable) {
int iLevel = 0;
LinkedList updateTables = new LinkedList(), oneLevel = new LinkedList();
oneLevel.add(firstTable);
updateTables.add(oneLevel);
while (intLevel < updateTables.size() {
//Go through all the tables in oneLevel and get a list of all their child
// child tables. Put them in a new LinkedList and add it to updateTables as
// the next node. If there are none, we don't add a node to updateTables so
// it doesn't increase in length, but intLevel will increase and be the
// same as the size of updateTables. That is the flag that we're out of
// dependent or child tables
intLevel++;
}
// code to go through updateTables and update all tables we've found.
return;
}

[More of explanation snipped for brevity]
....
The drawback of this algorithm is that you must always process the
entire tree. That is, you cannot, for example, only update B and its
dependents, because adding B to the queue will eventually trigger the
processing of E, but you'll miss the processing of F.

What I've ended up with is a routine that will start at any table, start
reading all the dependent ones from it, then get their dependents and so
on. It tracks which level each one is on and update them by going through
each level and updating each table in that level before moving on to the
next. I can start on the root table or on any other tables after it and
still do the same thing.
Having the dependency list as an acyclic graph allows you to only
process the parts of the trees that need to be processed.

At this point, I've got something that does close to that. Maybe I figured
it out without the actual terms?!

Thank you for all the time and effort I know it took to write up a clear
explanation. It was a help!

Hal
 
S

Stefan Ram

Hal Vaughan said:
I checked out the Wikipedia article on acyclic directed graphs.
In some ways that is close to what I was doing.

I have not fully understood your text about those tables yet.

But since you mention graphs: I have a package in ram.jar that
is intended to store arbitrary graphs. It is intended for
knowledge representation. It still lacks a serialization format.

A small introduction into this package:

http://www.purl.org/stefan_ram/pub/de.dclj.ram.system.space

The ram.jar

http://www.purl.org/stefan_ram/pub/ram-jar

A special property of this system is that any arrow can also
be used as an anchor point for another arrow. So there might
be an arrow from an arrow to another arrow, or from an arrow
to a point.

Picture: A Graph with five string points A, B, C, D, and E,
two point-to-point arrows b and d,
an arrow-to-arrow arrow cd, and an
arrow-to-point arrow E.

A C
| |
| cd | e
|----------->|---------->E
| |
|b |d
v V
B D


The rightmost arrow »e«, for example, might be used to label
the arrow »d« to the left of it as »E«.

This might also be used to implement relations with an arity
other than 2 by curryfication.

I still do not know, how such a graph (were arrows might start
or end at other arrows) is called.

The system is intended to support lookup by having each point
know about all its inarrows and it outarrows, so one can
easily enumerate them.
 
J

Jeff Higgins

Oliver said:
Having the dependency list as an acyclic graph allows you to only
process the parts of the trees that need to be processed.

Oliver,
Thanks for eye-opening(for me) explanation.
After reading your post I have been able to overcome a stumbling block
in one of my own back-burner projects, a Java impl of rmutt.

Googling on dag produced for me a package buried deep in Apache
Excalibur project with just the right dag impl for my purpose.

Very appreciative,
Jeff Higgins


public class DependancyTest
{
public static void main(String[] args)
{
DependancyModel model = new DependancyModel();

// ,-<-B<-.
// E<-\ / \
// *-<-*---<-C<---*-<-A
// F<-/ \ /
// `-<-D<-'

model.addDependant(new Dependant("A"), "root");
model.addDependant(new Dependant("B"), "A");
model.addDependant(new Dependant("C"), "A");
model.addDependant(new Dependant("D"), "A");
model.addDependant(new Dependant("E"),
new String[]{"A","B","C","D"});
model.addDependant(new Dependant("F"),
new String[] {"A","B","C","D"});
for(Dependant t : model.getDependancies("D"))
{
System.out.print(t.getName() + " ");
}
}
}

class Dependant
{
private String name;

public Dependant(String name)
{
this.name = name;
}

public String getName()
{
return name;
}
}

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.avalon.fortress.util.dag.Vertex;

public class DependancyModel
{
Map<String, Vertex> nameMap;
Vertex root;

public DependancyModel()
{
nameMap = new HashMap<String, Vertex>();
root = new Vertex(null, "root");
nameMap.put("root", root);
}

public void addDependant(Dependant dept, String anc)
{
Vertex vertex = new Vertex(dept.getName(), dept);
nameMap.get(anc).addDependency(vertex);
nameMap.put(dept.getName(), vertex);
}

public void addDependant(Dependant dept, String[] anc)
{
Vertex vertex = new Vertex(dept.getName(), dept);
for (String s : anc)
{
nameMap.get(s).addDependency(vertex);
nameMap.put(dept.getName(), vertex);
}
}

public List<Dependant> getDependancies(String deptName)
{
List<Vertex> vertices =
nameMap.get(deptName).getDependencies();
List<Dependant> tables = new ArrayList<Dependant>();
for (Vertex v : vertices)
{
tables.add((Dependant) v.getNode());
}
return tables;
}
}
 
J

Jeff Higgins

Lew said:
etc.

These would probably best be spelled with the name part "Dependancy" the
same as the corresponding English word, "dependency".


A parochial American would regard this as a misspelling; not so in the
U.K.

<http://en.wiktionary.org/wiki/dependant>

if intended as a noun.

huh?
You mean the global community considers my spelling, what,
correct but stuck-up, or incorrect and pretentious, or ...

I had a co-worker several years ago who used the phrase
anal-retentive exhaustivly, we're all still tired from it.

:0)
JH
 
T

Twisted

Basically, this program is a GUI that lets me edit the
settings quickly with point-and-click which, for me, is much faster than
dealing with all the SQL commands and the interrelations of the different
settings.

Aha! Another victory of the GUI over the command line.
 
H

Hal Vaughan

Twisted said:
Aha! Another victory of the GUI over the command line.

It's more complex than that. I've been building a data mining system for
over 5 years. When I started, I hadn't written a line of code for over a
decade. When I had been coding, I knew BASIC, AppleSoft, VAX 11/780
Assembler (from a class in college) and 6502 Assembler (on my Apple //e).
When I started this, I had to learn TCL (hated it!), Perl, JavaScript
(hated it!), Java, SQL and a few other languages for smaller tasks that I
can't remember right off.

By passion, I'm a script writer. When I have to make changes in MySQL to
change settings or add a client it can take hours or days because I have to
make sure when I change a setting, I change all the associated settings and
that everything is done in sync. With this Setting Editor, I can make most
of those changes in, literally, 2 minutes or less. Setting up a new client
will take less than 10 minutes and the server will automatically prepare
their software, upload it to the web site, and send them email notifying
them everything is ready.

It's been years since I had time for writing but when the Setting Editor is
done, I'm hoping I can handle all the management tasks for business in less
than one business day a week so I have the rest of the time for writing.

The command line is much more powerful and there's just no way I could make
the Setting Editor do everything with the settings or data that I can do
with just simple SELECT, DELETE and other basic SQL commands, but by now I
know what needs to be done and what doesn't, so I've set up ways to do most
of those tasks with commands and switches. The Setting Editor will not
only edit settings, but let me queue commands by task so I don't have to
spend time focused on the nitty gritty of the database when I'm done.

If I ever write the next Star Wars (okay, a bit of facetiousness here), then
I could honestly say that everyone who has helped me with advice about
programming has helped any movie I write or make get done. Oh, and after
years of this, I also know enough to understand that real science and math
can be exciting in a well done script and there's no need to ignore basic
physics or to trash science to make a story exciting.

Hal
 
B

blmblm

It's more complex than that.

[ snip ]
By passion, I'm a script writer.

[ snip ]

So of course you understand ....
The command line is much more powerful

[ snip ]
If I ever write the next Star Wars

Oh!! *That* kind of script writer!
(okay, a bit of facetiousness here), then
I could honestly say that everyone who has helped me with advice about
programming has helped any movie I write or make get done. Oh, and after
years of this, I also know enough to understand that real science and math
can be exciting in a well done script and there's no need to ignore basic
physics or to trash science to make a story exciting.

Cool -- good luck with it.
 
R

Roedy Green

That was one thing I considered, but was hoping to find one already
existing. With my limited experience, I'm surprised to find that there are
data types I know about that aren't easily implemented in Java.
DefaultTreeModel was close, but I could not get all the nodes in one level
easily.

if all you need in a extra method or two, just extend
DefaultTreeModel. You get a head start of all the rest well debugged.
 
T

Twisted

The command line is much more powerful and there's just no way I could make
the Setting Editor do everything with the settings or data that I can do
with just simple SELECT, DELETE and other basic SQL commands, but by now I
know what needs to be done and what doesn't, so I've set up ways to do most
of those tasks with commands and switches. The Setting Editor will not
only edit settings, but let me queue commands by task so I don't have to
spend time focused on the nitty gritty of the database when I'm done.

I rest my case -- the CLI is useful for the odd corner case but for
the most common cases having and using a GUI tool massively speeds
things up and simplifies them ergonomically.
 
T

Twisted

Hey, Hal, just remember - there's no sound in space.

Sure there is -- well, sort of. First, very long low-amplitude density
waves may propagate in the very rarefied matter of outer space.
Second, gravitational waves are longitudinal waves (much like sound
waves; contrast the transverse waves involved in light and radio) and
can in theory induce sound-like waves in matter (by stretching and
compressing it).
 
H

Hal Vaughan

Lew said:
Hal said:
By passion, I'm a script writer. [ snip ]
The command line is much more powerful [ snip ]
If I ever write the next Star Wars

Oh!! *That* kind of script writer!

Yeah - I kept thinking "Bash? TCL (he hates it)? Ruby?"

Yep. My bad. I keep the two associated differently. When I hear "script,"
I think film script first unless I'm specifically talking about Bash or
something like that.

Hey, Hal, just remember - there's no sound in space.

Is that why "In space, no one can hear you scream?"

Have you read what Joe Straczynski has said about that? When they started
work on Babylon 5, they sent out questionnaires to a number of scientists
about many points in FX and related topics to get their feedback on a lot
of similar issues. A number of scientists said that the "no sound in
space" was not that absolute. One point was that if one was close enough
to the explosion, sound would be carried with the expanding oxygen envelope
of an exploding ship.

Personally, I want to find a way to do them without the sound and still make
them effective. A lot can depend on the context.

Hal
 
H

Hal Vaughan

Twisted said:
I rest my case -- the CLI is useful for the odd corner case but for
the most common cases having and using a GUI tool massively speeds
things up and simplifies them ergonomically.

I used to teach Special Ed. I've found I can work well with a CLI or GUI
and tend to prefer one over the other depending on the task. If I had my
choice, I'd probably use a GUI whenever possible, since I'm a visual
thinker, but for some people, their style of learning and thinking lends it
much more to using a command line. It depends on their learning styles,
how they process data and if they think more with words or images. I had
to adapt lessons to how students perceived and learned to target words at
some students and visual lessons at others for just those reasons.

All that said, I look forward to when my system can be run entirely with a
GUI. Then when I'm programming, it's for fun stuff I want to do.

Hal
 
L

Lew

Twisted said:
Sure there is -- well, sort of. First, very long low-amplitude density
waves may propagate in the very rarefied matter of outer space.
Second, gravitational waves are longitudinal waves (much like sound
waves; contrast the transverse waves involved in light and radio) and
can in theory induce sound-like waves in matter (by stretching and
compressing it).

From the point of view of a script writer, only audible frequencies are of
interest.

"Sound" in a popular sense refers to audible sound waves; as in the Bishop
Berkeley conundrum, if there is no one to hear it, there is no sound.

You are correct, of course, if the context is the specialized scientific use
of the term "sound". Sound in that sense wouldn't influence a script or its
eventual special effects in any event.
 
L

Lew

Hal said:
Is that why "In space, no one can hear you scream?"

Have you read what Joe Straczynski has said about that? When they started
work on Babylon 5, they sent out questionnaires to a number of scientists
about many points in FX and related topics to get their feedback on a lot
of similar issues. A number of scientists said that the "no sound in
space" was not that absolute. One point was that if one was close enough
to the explosion, sound would be carried with the expanding oxygen envelope
of an exploding ship.

Which means you wouldn't hear it until that envelope reached you, unlike the
usual special effect of hearing it right away.
Personally, I want to find a way to do them without the sound and still make
them effective. A lot can depend on the context.

Ironically, Straczynski is about the only one to respect the silence of space,
in what I call the "Rosencrantz and Guildenstern" episode from the fifth
season of B5. They show explosions (far enough away from the space station to
be silent) but you can't hear them.

Anyway, absolute or not, mere consideration of the dictum "there is no sound
in space" (or the "can't hear you scream" variant) is enough to keep you
honest. Then you consider the exceptions, which are at best tertiary order,
and code for them accordingly. (Like waiting for the explosion to reach you
before hearing it.)

I had a cousin sell a movie script once. He advised me that one always takes
the cash, never points.
 
H

Hal Vaughan

Lew said:
Which means you wouldn't hear it until that envelope reached you, unlike
the usual special effect of hearing it right away.

Actually, yes, and that might make for a rather cool effect as it comes on
strong with the first wave, would include a wind like sound, and it would
taper off as the expanding cloud of air thinned.
Ironically, Straczynski is about the only one to respect the silence of
space, in what I call the "Rosencrantz and Guildenstern" episode from the
fifth
season of B5. They show explosions (far enough away from the space
station to be silent) but you can't hear them.

They also had space ships that actually followed the laws of physics in
their movement.
Anyway, absolute or not, mere consideration of the dictum "there is no
sound in space" (or the "can't hear you scream" variant) is enough to keep
you
honest. Then you consider the exceptions, which are at best tertiary
order,
and code for them accordingly. (Like waiting for the explosion to reach
you before hearing it.)

I had a cousin sell a movie script once. He advised me that one always
takes the cash, never points.

I won't be selling scripts. I'm starting with direct to DVD. I take into
account what I can do with a computer when I write. I'll be using Blender
for a lot of effects. The computer business will pay enough to let me hire
a cast and crew and build sets. Some of the first ideas I have work with
an alternate universe that's in a comic book like setting. That way I can
do FX that don't have to look real while I learn and practice, before I (or
people I get to work with me) get good enough to make it realistic.

Hal
 
M

Martin Gregorie

Lew said:
Hal said:
By passion, I'm a script writer. [ snip ]
The command line is much more powerful [ snip ]
If I ever write the next Star Wars

Oh!! *That* kind of script writer!

Yeah - I kept thinking "Bash? TCL (he hates it)? Ruby?"

Hey, Hal, just remember - there's no sound in space.
Maybe not in real space, but there is in Space Opera space: you can
always hear the mighty intergalactic space cruiser rumble past. In Space
Opera space the fighters can dogfight like WW1 planes too.

IMO realizing that there's this difference between space and Space Opera
is what made the first Star Wars movie so great.
 
H

Hal Vaughan

Martin said:
Lew said:
Hal said:
By passion, I'm a script writer.
[ snip ]
The command line is much more powerful
[ snip ]
If I ever write the next Star Wars

Oh!! *That* kind of script writer!

Yeah - I kept thinking "Bash? TCL (he hates it)? Ruby?"

Hey, Hal, just remember - there's no sound in space.
Maybe not in real space, but there is in Space Opera space: you can
always hear the mighty intergalactic space cruiser rumble past. In Space
Opera space the fighters can dogfight like WW1 planes too.

IMO realizing that there's this difference between space and Space Opera
is what made the first Star Wars movie so great.

That's one thing I was thinking about along the way. I would classify Star
Wars as fantasy, not science fiction and can forgive them for a few points
like sound in space, but I don't think the same rule would apply to Star
Trek or other shows that claim to be more accurate.

Hal
 
M

Martin Gregorie

Hal said:
Martin said:
Lew said:
Hal Vaughan wrote:
By passion, I'm a script writer.
[ snip ]
The command line is much more powerful
[ snip ]
If I ever write the next Star Wars
(e-mail address removed) wrote:
Oh!! *That* kind of script writer!
Yeah - I kept thinking "Bash? TCL (he hates it)? Ruby?"

Hey, Hal, just remember - there's no sound in space.
Maybe not in real space, but there is in Space Opera space: you can
always hear the mighty intergalactic space cruiser rumble past. In Space
Opera space the fighters can dogfight like WW1 planes too.

IMO realizing that there's this difference between space and Space Opera
is what made the first Star Wars movie so great.

That's one thing I was thinking about along the way. I would classify Star
Wars as fantasy, not science fiction and can forgive them for a few points
like sound in space, but I don't think the same rule would apply to Star
Trek or other shows that claim to be more accurate.
I never thought of it as fantasy. To me that involves magic and swords.
Space Opera is, or should be, very close to SF but it does have its own,
often tongue in cheek, rules and conventions. It can bend but not break
physics and biology, but it MUST be set in a self-consistent universe. I
think fantasy all too often breaks the last rule. Middle Earth and the
world of Barbarella share one major feature: both universes are just a
backdrop for the action. If you want to read first rate, self-consistent
fantasy you can't find better than Ursula LeGuin's Earthsea Trilogy.

But back to Space Opera. In the past I read more of it than was good for
me, so to me Star Wars fits right into the worlds of van Vogt (World of
Null-A, Weapon Shops of Esther, etc) and a lot of Harry Harrison (The
Stainless Steel Rat) and, of course, much Asimov (esp. the Foundation
series). I think Lucas also read a lot of it because there's no space
opera cliche left unturned in the first film. It was all the better for
that. Mind you, both Space and Horse Opera do have some fantasy
elements, witness the white-hat cowboy's deadly accurate, never reloaded
six-shooter. Not much different from a blaster or a light sword, really!
Similarly, Chewbacca and Tonto have a lot in common.

Good Space Opera isn't a past genre either. If you haven't read them, I
can recommend Ian M Bank's Culture novels, especially "Consider Phlebas"
and "Use of Weapons" as really enjoyable examples of the genre.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top