160 likes | 178 Views
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.
E N D
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
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
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
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)
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
Multigraph and Psuedograph A A B C B C D D Multigraph Psuedograph
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
Weighted Graph A 8 5 E B 9 C 7 5 4 2 D F
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
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
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
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
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
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
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