对象图比较重presentation以邻接表和矩阵重新presentations [英] Comparing object graph representation to adjacency list and matrix representations

查看:144
本文介绍了对象图比较重presentation以邻接表和矩阵重新presentations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在跟踪史蒂夫Yegge对preparing的技术编程面试建议:<一href="http://steve-yegge.blogspot.com/2008/03/get-that-job-at-google.html">http://steve-yegge.blogspot.com/2008/03/get-that-job-at-google.html

I'm currently following Steve Yegge's advice on preparing for a technical programming interview: http://steve-yegge.blogspot.com/2008/03/get-that-job-at-google.html

在他的上图部分,他说:

In his section on Graphs, he states:

有三种基本途径   重新present在存储器的图表(对象   和指针,矩阵和邻接   列表),并且你应该熟悉   自己每次重新presentation和   它的优点和缺点。

There are three basic ways to represent a graph in memory (objects and pointers, matrix, and adjacency list), and you should familiarize yourself with each representation and its pros and cons.

矩阵和邻接表重新presentations的利弊CLRS描述,但我一直没能找到比较这些为对象重新presentation资源。

The pros and cons of matrix and adjacency list representations are described in CLRS, but I haven't been able to find a resource that compares these to an object representation.

仅仅通过想着它,我可以推断一些这方面我自己,但我想,以确保我没有错过一些重要的事情。如果有人能形容这个COM prehensively,或点我到这样做的资源,我将不胜AP preciate吧。

Just by thinking about it, I can infer some of this myself, but I'd like to make sure I haven't missed something important. If someone could describe this comprehensively, or point me to a resource which does so, I would greatly appreciate it.

推荐答案

对象和指针

这些都只是基本的数据结构一样HAMMAR在对方回答说,在的Java ,你会重新使用类,如边和顶点present这一点。例如一条边连接两个顶点,可以将其向或无向,它可以包含一个重量。一个顶点可以有一个ID,姓名等天色他们都有附加属性。所以,你可以建立你的图,他们像

These are just basic datastructures like hammar said in the other answer, in Java you would represent this with classes like edges and vertices. For example an edge connects two vertices and can either be directed or undirected and it can contain a weight. A vertex can have an ID, name etc. Mostly both of them have additional properties. So you can construct your graph with them like

Vertex a = new Vertex(1);
Vertex b = new Vertex(2);
Edge edge = new Edge(a,b, 30); // init an edge between ab and be with weight 30  

此方法通常用于面向对象的实现,因为它是更具有可读性和方便的面向对象的用户;。)

This approach is commonly used for object oriented implementations, since it is more readable and convenient for object oriented users ;).

矩阵

一个矩阵只是一个简单的二维数组。假设你有一个可以重新psented作为一个int数组像这样$ P $顶点的ID:

A matrix is just a simple 2 dimensional array. Assuming you have vertex ID's that can be represented as an int array like this:

int[][] adjacencyMatrix = new int[SIZE][SIZE]; // SIZE is the number of vertices in our graph
adjacencyMatrix[0][1] = 30; // sets the weight of a vertex 0 that is adjacent to vertex 1

这是通常用于密集的图形,其中索引访问是必要的。您可以重新present联合国/定向和加权结构与此有关。

This is commonly used for dense graphs where index access is necessary. You can represent a un/directed and weighted structure with this.

邻接表

这只是一个简单的数据结构组合,我通常使用的HashMap&LT实现这一点;顶点,名单,其中,顶点&GT;&GT; 。类似的使用可以是 HashMultimap 的番石榴。

This is just a simple datastructure mix, I usually implement this using a HashMap<Vertex, List<Vertex>>. Similar used can be the HashMultimap in Guava.

此方法是很酷的,因为你有O(1)(摊销)顶点查找并返回我的所有相邻顶点的列表,以这种特殊的顶点我的要求。

This approach is cool, because you have O(1) (amortized) vertex lookup and it returns me a list of all adjacent vertices to this particular vertex I demanded.

ArrayList<Vertex> list = new ArrayList<>();
list.add(new Vertex(2));
list.add(new Vertex(3));
map.put(new Vertex(1), list); // vertex 1 is adjacent to 2 and 3

这是用于重presenting稀疏图,如果你申请在谷歌,你应该知道,webgraph稀疏。你可以与他们使用的是的BigTable 处理更可扩展的方式。

This is used for representing sparse graphs, if you are applying at Google, you should know that the webgraph is sparse. You can deal with them in a more scalable way using a BigTable.

呵呵,并顺便说一句,<一个href="http://java.dzone.com/articles/algorithm-week-graphs-and?utm_source=twitterfeed&utm_medium=twitter&utm_campaign=Feed%3A+javalobby%2Ffrontpage+%28Javalobby+%2F+Java+Zone%29">here就是这个帖子用花哨的图片一个很好的总结;)

Oh and BTW, here is a very good summary of this post with fancy pictures ;)

这篇关于对象图比较重presentation以邻接表和矩阵重新presentations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆