Graph Representations - PowerPoint PPT Presentation

About This Presentation
Title:

Graph Representations

Description:

Lecture 14 Graph Representations Graph Representation How do we represent a graph internally? Two ways adjacency matrix list Adjacency Matrix For each edge (v,w) in E ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 7
Provided by: DonSl1
Category:

less

Transcript and Presenter's Notes

Title: Graph Representations


1
Lecture 14
  • Graph Representations

2
Graph Representation
  • How do we represent a graph internally?
  • Two ways
  • adjacency matrix
  • list
  • Adjacency Matrix
  • For each edge (v,w) in E, set Avw edge_cost
  • Non existent edges with logical infinity
  • Cost of implementation
  • O(V2) time for initialization
  • O(V2) space
  • ok for dense graphs
  • unacceptable for sparse graphs

3
Graph Class (Matrix representation)
  • Public class Graph
  • public static final double NO_EDGE Graph()
  • Graph(int n)
  • public void SetSize(int n)
  • public void AddEdge(int origin, int
    destination, double value)
  • public void RemoveEdge(int origin, int
    destination)
  • public boolean HasEdge(int origin, int
    destination)
  • public double EdgeValue(int origin, int
    destination)
  • int NumNodes()
  • int NumEdges()
  • private int myNumEdges
  • private int M

4
Graph Representation
  • Adjacency List
  • Ideal solution for sparse graphs
  • For each vertex keep a list of all adjacent
    vertices
  • Adjacent vertices are the vertices that are
    connected to the vertex directly by an edge.
  • Example
  • List 0
  • List 1
  • List 2
  • Draw the graph ??

1
2
2
0
1
1
5
Graph Representation ctd..
  • The number of list nodes equals to number of
    edges
  • O(E) space
  • Space is also required to store the lists
  • O(V) for V lists
  • Note that the number of edges is at least
    round(V/2)
  • assuming each vertex is in some edge
  • Therefore disregard any O(V) term when O(E)
    is present
  • Adjacency list can be constructed in linear time
    (wrt to edges)
  • More on this when we learn pointers

6
Graph Algorithms
  • Graphs depend on two parameters
  • edges (E)
  • Vertices (V)
  • Graph algorithms can be complicated to analyze
  • One algorithm might be order (V2)
  • for dense graphs
  • Another might be order((EV)log E)
  • for sparse graphs
  • Depth First Search
  • Is the graph connected? If not what are their
    connected components?
  • Does the graph have a cycle?
  • How do we examine (visit) every node and every
    edge systematically?
  • First select a vertex, set all vertices connected
    to that vertex to non-zero
  • Find a vertex that has not been visited and
    repeat the step above
  • Repeat above until all zero vertices are
    examined.
Write a Comment
User Comments (0)
About PowerShow.com