1 / 16

Graphs – Part I

Graphs – Part I. CS 367 – Introduction to Data Structures. Graph Definitions. Vertices nodes in the graph Edges connection from one node to another General graph definition a graph is a collection of vertices and edges with little or no restrictions on which vertices are connected together.

alexpeters
Download Presentation

Graphs – Part I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphs – Part I CS 367 – Introduction to Data Structures

  2. Graph Definitions • Vertices • nodes in the graph • Edges • connection from one node to another • General graph definition • a graph is a collection of vertices and edges with little or no restrictions on which vertices are connected together

  3. Graph vs. Trees • In theory, a tree is a restricted graph • it is laid out in a hierarchical manner • each node can have only one parent • each node has a set of children from 0 to n • siblings have no direct connection • In a graph, a “child” can connect directly to its “sibling” and even to its “parent” • the quotes are provided because, unlike trees, there is no notion of parent and child

  4. Simple and Directed Graphs • A simple graph has a set of vertices connected by a set of edges • no notion of direction from one vertex to another • an edge from vm to vn is considered the same as and edge from vn to vm • A directed graph (or digraph) also has a set of vertices connected by a set of edges • now there is a notion of direction • an edge from vm to vn is NOT the same as and edge from vn to vm

  5. Simple and Directed Graphs A A E E B C B C G F G D D F Simple Graph (B->D) == (D->B) Directed Graph (B->D) != (D->B) (in fact, there is no edge D->B)

  6. Multigraph and Psuedograph • A multigraph allows more than one edge between two vertices • an edge must have two different vertices on each end • A psuedograph is exactly like a multigraph except it allows an edge to have the same vertex at both ends • this is called a cycle

  7. Multigraph and Psuedograph A A B C B C D D Multigraph Psuedograph

  8. Weighted Graph • A graph where values are associated with each edge • usually considered the “cost” of moving from one vertex to another vertex • any type of graph can be converted into a weighted graph

  9. Weighted Graph A 8 5 E B 9 C 7 5 4 2 D F

  10. Representing a Graph • Several ways to represent a graph • need to store vertices • need a way to represent edges • Two possibilities • adjacency list • adjacency matrix

  11. Adjacency List • Stores a list of all vertices adjacent to each vertex • can do this in a table or a linked list • Using a table • simple solution is to use a 2-D array • could use an array (or vector) where each element contains vertex and list • Using a linked list • each node contains two pointers • one pointer to the next vertex • one pointer to a list of adjacent vertices

  12. Adjacency List – Table A B C D A B A D F E C A D B C D A B C F D E F F B E

  13. Adjacency List – Linked List A B C D B A D F A E C A D B C D A B C F D E F F B E

  14. Adjacency Matrix • Implemented using a two dimensional square array • each vertex is listed as a row • each vertex is listed as a column • a 1 in any any element (i, j) indicates the two nodes are adjacent to each other

  15. Adjacency Matrix A B C D E F A 0 1 1 1 0 0 A E B 1 0 0 1 0 1 B C C 1 0 0 1 0 0 D 1 1 1 0 0 0 F D E 0 0 0 0 0 1 F 0 1 0 0 1 0

  16. Which Representation? • Which method to use depends on what is being done • processing vertices adjacent to a vertex? • quicker to use an adjacency list • removing edges? • quicker to use an adjacency matrix

More Related