HTML

html content help to improve the coding

Sunday, 1 April 2012

GRAPH IN DATA STRUCTURE


Graph (abstract data type)

From Wikipedia, the free encyclopedia
A labeled graph of 6 vertices and 7 edges.
In computer science, a graph is an abstract data type that is meant to implement the graph and hypergraph concepts from mathematics.
A graph data structure consists of a finite (and possibly mutable) set of ordered pairs, called edges or arcs, of certain entities called nodes or vertices. As in mathematics, an edge (x,y) is said to point or go from x to y. The nodes may be part of the graph structure, or may be external entities represented by integer indices or references.
A graph data structure may also associate to each edge some edge value, such as a symbolic label or a numeric attribute (cost, capacity, length, etc.).

Contents

  [hide

[edit]Algorithms

Graph algorithms are a significant field of interest within computer science. Typical higher-level operations associated with graphs are: finding a path between two nodes, like depth-first search andbreadth-first search and finding the shortest path from one node to another, like Dijkstra's algorithm. A solution to finding the shortest path from each node to every other node also exists in the form of the Floyd–Warshall algorithm.
directed graph can be seen as a flow network, where each edge has a capacity and each edge receives a flow. The Ford–Fulkerson algorithm is used to find out the maximum flow from a source to a sink in a graph.

[edit]Operations

The basic operations provided by a graph data structure G usually include:
  • adjacent(Gxy): tests whether there is an edge from node x to node y.
  • neighbors(Gx): lists all nodes y such that there is an edge from x to y.
  • add(Gxy): adds to G the edge from x to y, if it is not there.
  • delete(Gxy): removes the edge from x to y, if it is there.
  • get_node_value(Gx): returns the value associated with the node x.
  • set_node_value(Gxa): sets the value associated with the node x to a.
Structures that associate values to the edges usually also provide:
  • get_edge_value(Gxy): returns the value associated to the edge (x,y).
  • set_edge_value(Gxyv): sets the value associated to the edge (x,y) to v.

[edit]Representations

Different data structures for the representation of graphs are used in practice:
  • Adjacency list – Vertices are stored as records or objects, and every vertex stores a list of adjacent vertices. This data structure allows the storage of additional data on the vertices.
  • Incidence list – Vertices and edges are stored as records or objects. Each vertex stores its incident edges, and each edge stores its incident vertices. This data structure allows the storage of additional data on vertices and edges.
  • Adjacency matrix – A two-dimensional matrix, in which the rows represent source vertices and columns represent destination vertices. Data on edges and vertices must be stored externally. Only the cost for one edge can be stored between each pair of vertices.
  • Incidence matrix – A two-dimensional Boolean matrix, in which the rows represent the vertices and columns represent the edges. The entries indicate whether the vertex at a row is incident to the edge at a column.
The following table gives the time complexity cost of performing various operations on graphs, for each of these representations.[citation needed] In the matrix representations, the entries encode the cost of following an edge. The cost of edges that are not present are assumed to be  \infty .
Adjacency listIncidence listAdjacency matrixIncidence matrix
Storage O(|V|+|E|)  O(|V|+|E|)  O(|V|^2)  O(|V|\cdot|E|)
Add vertex O(1)  O(1)  O(|V|^2)  O(|V|\cdot|E|)
Add edge O(1)  O(1)  O(1)  O(|V|\cdot|E|)
Remove vertex O(|E|)  O(|E|)  O(|V|^2)  O(|V|\cdot|E|)
Remove edge O(|E|)  O(|E|)  O(1)  O(|V|\cdot|E|)
Query: are vertices u, v adjacent? (Assuming that the storage positions for u, v are known) O(|E|)  O(|E|)  O(1)  O(|E|)
RemarksWhen removing edges or vertices, need to find all vertices or edgesSlow to add or remove vertices, because matrix must be resized/copiedSlow to add or remove vertices and edges, because matrix must be resized/copied
Adjacency lists are generally preferred because they efficiently represent sparse graphs. An adjacency matrix is preferred if the graph is dense, that is the number of edges E is close to the number of vertices squared, V2, or if one must be able to quickly look up if there is an edge connecting two vertices.[1]

No comments:

Post a Comment