SlideShare a Scribd company logo
I'm having trouble figuring out how to code these sections for an assignment... (Sections 4 and 5
are in a .cpp file without a main. Section 6 is in a seperate file that's only purpose is to test using
a main method.) For further information, a link to the assignment can be found here -
http://www.cs.ecu.edu/~karl/2530/fall16/Assn/Assn6/assn6.html
4. Reading the Graph
Document and define a function to read a graph. Use the adjacency list representation. Only store
the graph once.
5. Printing a Graph
Document and define a function to print a graph. .
6. Testing
Test reading and echoing the graph. Do not move on until you are satisfied that this much works.
(Testing is done in the main method, not the .cpp file).
How would I code this using the following code?
#include
using namespace std;
struct Edge{
int vertexNumber;
int weight;
struct Edge* next;
Edge( int vn, int wgt, struct Edge* nxt ){
vertexNumber = vn;
weight = wgt;
next = nxt;
};
};
struct Vertex{
struct Edge* adjacencyList;
double shortestDistance;
int vertexNumber;
Vertex(){
vertexNumber = shortestDistance = -1;
adjacencyList = NULL;
}
};
struct Graph{
int nVertices;
int nEdges;
struct Vertex* vertices;
Graph( int nVert ){
vertices = new struct Vertex[nVert];
nEdges = 0;
}
};
typedef struct Edge Edge;
typedef struct Vertex Vertex;
typedef struct Graph Graph;
int main(){
}
4. Reading the Graph
Document and define a function to read a graph. Use the adjacency list representation. Only
store the graph once.
5. Printing a Graph
Document and define a function to print a graph. .
6. Testing
Test reading and echoing the graph. Do not move on until you are satisfied that this much
works.
(Testing is done in the main method, not the .cpp file).
Solution
4)
// A C Program to demonstrate adjacency list representation of graphs
#include
#include
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
struct AdjListNode* next;
};
// A structure to represent an adjacency list
struct AdjList
{
struct AdjListNode *head; // pointer to head node of list
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest)
{
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
// Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest)
{
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
// Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
// A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
struct AdjListNode* pCrawl = graph->array[v].head;
printf(" Adjacency list of vertex %d head ", v);
while (pCrawl)
{
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf(" ");
}
}
// Driver program to test above functions
int main()
{
// create the graph given in above fugure
int V = 5;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
// print the adjacency list representation of the above graph
printGraph(graph);
return 0;
}
5) to print use this code in c
// C++ program to print all paths from a source to destination.
#include
#include
using namespace std;
// A directed graph using adjacency list representation
class Graph
{
int V; // No. of vertices in graph
list *adj; // Pointer to an array containing adjacency lists
// A recursive function used by printAllPaths()
void printAllPathsUtil(int , int , bool [], int [], int &);
public:
Graph(int V); // Constructor
void addEdge(int u, int v);
void printAllPaths(int s, int d);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list[V];
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v); // Add v to u’s list.
}
// Prints all paths from 's' to 'd'
void Graph::printAllPaths(int s, int d)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
// Create an array to store paths
int *path = new int[V];
int path_index = 0; // Initialize path[] as empty
// Initialize all vertices as not visited
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function to print all paths
printAllPathsUtil(s, d, visited, path, path_index);
}
// A recursive function to print all paths from 'u' to 'd'.
// visited[] keeps track of vertices in current path.
// path[] stores actual vertices and path_index is current
// index in path[]
void Graph::printAllPathsUtil(int u, int d, bool visited[],
int path[], int &path_index)
{
// Mark the current node and store it in path[]
visited[u] = true;
path[path_index] = u;
path_index++;
// If current vertex is same as destination, then print
// current path[]
if (u == d)
{
for (int i = 0; i::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (!visited[*i])
printAllPathsUtil(*i, d, visited, path, path_index);
}
// Remove current vertex from path[] and mark it as unvisited
path_index--;
visited[u] = false;
}
// Driver program
int main()
{
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(2, 0);
g.addEdge(2, 1);
g.addEdge(1, 3);
int s = 2, d = 3;
cout << "Following are all different paths from " << s
<< " to " << d << endl;
g.printAllPaths(s, d);
return 0;
}
6)
echo “set samples 2000;plot abs(sin(x));rep abs(cos(x))”|gnuplot -persist
// A C Program to demonstrate adjacency list representation of graphs
#include
#include
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
struct AdjListNode* next;
};
// A structure to represent an adjacency list
struct AdjList
{
struct AdjListNode *head; // pointer to head node of list
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest)
{
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
// Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest)
{
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
// Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
// A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
struct AdjListNode* pCrawl = graph->array[v].head;
printf(" Adjacency list of vertex %d head ", v);
while (pCrawl)
{
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf(" ");
}
}
// Driver program to test above functions
int main()
{
// create the graph given in above fugure
int V = 5;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
// print the adjacency list representation of the above graph
printGraph(graph);
return 0;
}
5) to print use this code in c
// C++ program to print all paths from a source to destination.
#include
#include
using namespace std;
// A directed graph using adjacency list representation
class Graph
{
int V; // No. of vertices in graph
list *adj; // Pointer to an array containing adjacency lists
// A recursive function used by printAllPaths()
void printAllPathsUtil(int , int , bool [], int [], int &);
public:
Graph(int V); // Constructor
void addEdge(int u, int v);
void printAllPaths(int s, int d);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list[V];
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v); // Add v to u’s list.
}
// Prints all paths from 's' to 'd'
void Graph::printAllPaths(int s, int d)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
// Create an array to store paths
int *path = new int[V];
int path_index = 0; // Initialize path[] as empty
// Initialize all vertices as not visited
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function to print all paths
printAllPathsUtil(s, d, visited, path, path_index);
}
// A recursive function to print all paths from 'u' to 'd'.
// visited[] keeps track of vertices in current path.
// path[] stores actual vertices and path_index is current
// index in path[]
void Graph::printAllPathsUtil(int u, int d, bool visited[],
int path[], int &path_index)
{
// Mark the current node and store it in path[]
visited[u] = true;
path[path_index] = u;
path_index++;
// If current vertex is same as destination, then print
// current path[]
if (u == d)
{
for (int i = 0; i::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (!visited[*i])
printAllPathsUtil(*i, d, visited, path, path_index);
}
// Remove current vertex from path[] and mark it as unvisited
path_index--;
visited[u] = false;
}
// Driver program
int main()
{
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(2, 0);
g.addEdge(2, 1);
g.addEdge(1, 3);
int s = 2, d = 3;
cout << "Following are all different paths from " << s
<< " to " << d << endl;
g.printAllPaths(s, d);
return 0;
}
6)
echo “set samples 2000;plot abs(sin(x));rep abs(cos(x))”|gnuplot -persist

More Related Content

DOCX
This code currently works. Run it and get a screen shot of its ou.docx
DOCX
This code currently works... Run it and get a screen shot of its .docx
PDF
implement the following funtions. myg1 and myg2 are seperate. x and .pdf
PDF
The graph above is just an example that shows the differences in dis.pdf
PPT
Graphs.ppt of mathemaics we have to clar all doubts
PPT
Graphs.ppt
PPT
Chap 6 Graph.ppt
PPTX
Data Structures and Agorithm: DS 21 Graph Theory.pptx
This code currently works. Run it and get a screen shot of its ou.docx
This code currently works... Run it and get a screen shot of its .docx
implement the following funtions. myg1 and myg2 are seperate. x and .pdf
The graph above is just an example that shows the differences in dis.pdf
Graphs.ppt of mathemaics we have to clar all doubts
Graphs.ppt
Chap 6 Graph.ppt
Data Structures and Agorithm: DS 21 Graph Theory.pptx

Similar to Im having trouble figuring out how to code these sections for an a.pdf (20)

PPT
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
PPT
Graphs (1)
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
PPTX
Data Structure and algorithms - Graph1.pptx
PPTX
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
PPTX
DATA STRUCTURES unit 4.pptx
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH
PPT
lec 09-graphs-bfs-dfs.ppt
PPT
Graphs concept in data structures and algorithms.ppt
PPT
Lecture 5b graphs and hashing
PDF
graph representation.pdf
PPT
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
PPTX
DATA STRUCTURES.pptx
PPT
lecture 17
PPTX
ExploringPrimsAlgorithmforMinimumSpanningTreesinC.pptx
PPT
Dsa.PPT
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
Graphs (1)
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
Data structure Graph PPT ( BFS & DFS ) NOTES
Data Structure and algorithms - Graph1.pptx
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
DATA STRUCTURES unit 4.pptx
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH
lec 09-graphs-bfs-dfs.ppt
Graphs concept in data structures and algorithms.ppt
Lecture 5b graphs and hashing
graph representation.pdf
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
DATA STRUCTURES.pptx
lecture 17
ExploringPrimsAlgorithmforMinimumSpanningTreesinC.pptx
Dsa.PPT

More from rishteygallery (8)

PDF
How many four-digit numbers are there formed from the digits 1, 2, 3.pdf
PDF
If compound inhibits cilia growth, what is the molecular mechanism.pdf
PDF
I wrote the following change it to having a header, main and cpp fi.pdf
PDF
Green fluorescent protein is commonly used to label DNA sequences. .pdf
PDF
How can I align a face according to another face using eyes landma.pdf
PDF
Harriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdf
PDF
Edouard Manets painting The Bar at the Folies Bergeres shows a girl.pdf
PDF
Does a cell in your big toe contain a gene for insulinA. Yes.B..pdf
How many four-digit numbers are there formed from the digits 1, 2, 3.pdf
If compound inhibits cilia growth, what is the molecular mechanism.pdf
I wrote the following change it to having a header, main and cpp fi.pdf
Green fluorescent protein is commonly used to label DNA sequences. .pdf
How can I align a face according to another face using eyes landma.pdf
Harriet Knox, Ralph Patton and Marcia Diamond work for a family phys.pdf
Edouard Manets painting The Bar at the Folies Bergeres shows a girl.pdf
Does a cell in your big toe contain a gene for insulinA. Yes.B..pdf

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
PPTX
Cell Structure & Organelles in detailed.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
master seminar digital applications in india
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Institutional Correction lecture only . . .
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.
Cell Structure & Organelles in detailed.
GDM (1) (1).pptx small presentation for students
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
RMMM.pdf make it easy to upload and study
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
master seminar digital applications in india
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Institutional Correction lecture only . . .
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharma ospi slides which help in ospi learning
Microbial disease of the cardiovascular and lymphatic systems
Microbial diseases, their pathogenesis and prophylaxis
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx

Im having trouble figuring out how to code these sections for an a.pdf

  • 1. I'm having trouble figuring out how to code these sections for an assignment... (Sections 4 and 5 are in a .cpp file without a main. Section 6 is in a seperate file that's only purpose is to test using a main method.) For further information, a link to the assignment can be found here - http://www.cs.ecu.edu/~karl/2530/fall16/Assn/Assn6/assn6.html 4. Reading the Graph Document and define a function to read a graph. Use the adjacency list representation. Only store the graph once. 5. Printing a Graph Document and define a function to print a graph. . 6. Testing Test reading and echoing the graph. Do not move on until you are satisfied that this much works. (Testing is done in the main method, not the .cpp file). How would I code this using the following code? #include using namespace std; struct Edge{ int vertexNumber; int weight; struct Edge* next; Edge( int vn, int wgt, struct Edge* nxt ){ vertexNumber = vn; weight = wgt; next = nxt; }; }; struct Vertex{ struct Edge* adjacencyList; double shortestDistance; int vertexNumber; Vertex(){ vertexNumber = shortestDistance = -1; adjacencyList = NULL; } }; struct Graph{
  • 2. int nVertices; int nEdges; struct Vertex* vertices; Graph( int nVert ){ vertices = new struct Vertex[nVert]; nEdges = 0; } }; typedef struct Edge Edge; typedef struct Vertex Vertex; typedef struct Graph Graph; int main(){ } 4. Reading the Graph Document and define a function to read a graph. Use the adjacency list representation. Only store the graph once. 5. Printing a Graph Document and define a function to print a graph. . 6. Testing Test reading and echoing the graph. Do not move on until you are satisfied that this much works. (Testing is done in the main method, not the .cpp file). Solution 4) // A C Program to demonstrate adjacency list representation of graphs #include #include // A structure to represent an adjacency list node struct AdjListNode { int dest; struct AdjListNode* next; }; // A structure to represent an adjacency list
  • 3. struct AdjList { struct AdjListNode *head; // pointer to head node of list }; // A structure to represent a graph. A graph is an array of adjacency lists. // Size of array will be V (number of vertices in graph) struct Graph { int V; struct AdjList* array; }; // A utility function to create a new adjacency list node struct AdjListNode* newAdjListNode(int dest) { struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); newNode->dest = dest; newNode->next = NULL; return newNode; } // A utility function that creates a graph of V vertices struct Graph* createGraph(int V) { struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph)); graph->V = V; // Create an array of adjacency lists. Size of array will be V graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList)); // Initialize each adjacency list as empty by making head as NULL int i; for (i = 0; i < V; ++i) graph->array[i].head = NULL; return graph; } // Adds an edge to an undirected graph void addEdge(struct Graph* graph, int src, int dest) {
  • 4. // Add an edge from src to dest. A new node is added to the adjacency // list of src. The node is added at the begining struct AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; // Since graph is undirected, add an edge from dest to src also newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } // A utility function to print the adjacenncy list representation of graph void printGraph(struct Graph* graph) { int v; for (v = 0; v < graph->V; ++v) { struct AdjListNode* pCrawl = graph->array[v].head; printf(" Adjacency list of vertex %d head ", v); while (pCrawl) { printf("-> %d", pCrawl->dest); pCrawl = pCrawl->next; } printf(" "); } } // Driver program to test above functions int main() { // create the graph given in above fugure int V = 5; struct Graph* graph = createGraph(V); addEdge(graph, 0, 1); addEdge(graph, 0, 4); addEdge(graph, 1, 2); addEdge(graph, 1, 3);
  • 5. addEdge(graph, 1, 4); addEdge(graph, 2, 3); addEdge(graph, 3, 4); // print the adjacency list representation of the above graph printGraph(graph); return 0; } 5) to print use this code in c // C++ program to print all paths from a source to destination. #include #include using namespace std; // A directed graph using adjacency list representation class Graph { int V; // No. of vertices in graph list *adj; // Pointer to an array containing adjacency lists // A recursive function used by printAllPaths() void printAllPathsUtil(int , int , bool [], int [], int &); public: Graph(int V); // Constructor void addEdge(int u, int v); void printAllPaths(int s, int d); }; Graph::Graph(int V) { this->V = V; adj = new list[V]; } void Graph::addEdge(int u, int v) { adj[u].push_back(v); // Add v to u’s list. } // Prints all paths from 's' to 'd' void Graph::printAllPaths(int s, int d) {
  • 6. // Mark all the vertices as not visited bool *visited = new bool[V]; // Create an array to store paths int *path = new int[V]; int path_index = 0; // Initialize path[] as empty // Initialize all vertices as not visited for (int i = 0; i < V; i++) visited[i] = false; // Call the recursive helper function to print all paths printAllPathsUtil(s, d, visited, path, path_index); } // A recursive function to print all paths from 'u' to 'd'. // visited[] keeps track of vertices in current path. // path[] stores actual vertices and path_index is current // index in path[] void Graph::printAllPathsUtil(int u, int d, bool visited[], int path[], int &path_index) { // Mark the current node and store it in path[] visited[u] = true; path[path_index] = u; path_index++; // If current vertex is same as destination, then print // current path[] if (u == d) { for (int i = 0; i::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) if (!visited[*i]) printAllPathsUtil(*i, d, visited, path, path_index); } // Remove current vertex from path[] and mark it as unvisited path_index--; visited[u] = false; } // Driver program
  • 7. int main() { // Create a graph given in the above diagram Graph g(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(0, 3); g.addEdge(2, 0); g.addEdge(2, 1); g.addEdge(1, 3); int s = 2, d = 3; cout << "Following are all different paths from " << s << " to " << d << endl; g.printAllPaths(s, d); return 0; } 6) echo “set samples 2000;plot abs(sin(x));rep abs(cos(x))”|gnuplot -persist // A C Program to demonstrate adjacency list representation of graphs #include #include // A structure to represent an adjacency list node struct AdjListNode { int dest; struct AdjListNode* next; }; // A structure to represent an adjacency list struct AdjList { struct AdjListNode *head; // pointer to head node of list }; // A structure to represent a graph. A graph is an array of adjacency lists. // Size of array will be V (number of vertices in graph) struct Graph {
  • 8. int V; struct AdjList* array; }; // A utility function to create a new adjacency list node struct AdjListNode* newAdjListNode(int dest) { struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); newNode->dest = dest; newNode->next = NULL; return newNode; } // A utility function that creates a graph of V vertices struct Graph* createGraph(int V) { struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph)); graph->V = V; // Create an array of adjacency lists. Size of array will be V graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList)); // Initialize each adjacency list as empty by making head as NULL int i; for (i = 0; i < V; ++i) graph->array[i].head = NULL; return graph; } // Adds an edge to an undirected graph void addEdge(struct Graph* graph, int src, int dest) { // Add an edge from src to dest. A new node is added to the adjacency // list of src. The node is added at the begining struct AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; // Since graph is undirected, add an edge from dest to src also newNode = newAdjListNode(src); newNode->next = graph->array[dest].head;
  • 9. graph->array[dest].head = newNode; } // A utility function to print the adjacenncy list representation of graph void printGraph(struct Graph* graph) { int v; for (v = 0; v < graph->V; ++v) { struct AdjListNode* pCrawl = graph->array[v].head; printf(" Adjacency list of vertex %d head ", v); while (pCrawl) { printf("-> %d", pCrawl->dest); pCrawl = pCrawl->next; } printf(" "); } } // Driver program to test above functions int main() { // create the graph given in above fugure int V = 5; struct Graph* graph = createGraph(V); addEdge(graph, 0, 1); addEdge(graph, 0, 4); addEdge(graph, 1, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 3); addEdge(graph, 3, 4); // print the adjacency list representation of the above graph printGraph(graph); return 0; } 5) to print use this code in c
  • 10. // C++ program to print all paths from a source to destination. #include #include using namespace std; // A directed graph using adjacency list representation class Graph { int V; // No. of vertices in graph list *adj; // Pointer to an array containing adjacency lists // A recursive function used by printAllPaths() void printAllPathsUtil(int , int , bool [], int [], int &); public: Graph(int V); // Constructor void addEdge(int u, int v); void printAllPaths(int s, int d); }; Graph::Graph(int V) { this->V = V; adj = new list[V]; } void Graph::addEdge(int u, int v) { adj[u].push_back(v); // Add v to u’s list. } // Prints all paths from 's' to 'd' void Graph::printAllPaths(int s, int d) { // Mark all the vertices as not visited bool *visited = new bool[V]; // Create an array to store paths int *path = new int[V]; int path_index = 0; // Initialize path[] as empty // Initialize all vertices as not visited for (int i = 0; i < V; i++) visited[i] = false;
  • 11. // Call the recursive helper function to print all paths printAllPathsUtil(s, d, visited, path, path_index); } // A recursive function to print all paths from 'u' to 'd'. // visited[] keeps track of vertices in current path. // path[] stores actual vertices and path_index is current // index in path[] void Graph::printAllPathsUtil(int u, int d, bool visited[], int path[], int &path_index) { // Mark the current node and store it in path[] visited[u] = true; path[path_index] = u; path_index++; // If current vertex is same as destination, then print // current path[] if (u == d) { for (int i = 0; i::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) if (!visited[*i]) printAllPathsUtil(*i, d, visited, path, path_index); } // Remove current vertex from path[] and mark it as unvisited path_index--; visited[u] = false; } // Driver program int main() { // Create a graph given in the above diagram Graph g(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(0, 3); g.addEdge(2, 0);
  • 12. g.addEdge(2, 1); g.addEdge(1, 3); int s = 2, d = 3; cout << "Following are all different paths from " << s << " to " << d << endl; g.printAllPaths(s, d); return 0; } 6) echo “set samples 2000;plot abs(sin(x));rep abs(cos(x))”|gnuplot -persist